Help I can get the first GUI to pop up but not anything else

Help! I can get the first GUI to pop up but not anything else. Can you tell me where my code is wrong.

import java.util.Scanner;
import javax.swing.*;


public class Automobile {
   private String make; //Make of the AutoMobile
   private String color; //Color of the vehicle
   //to read input from the keyboard
   private java.util.Scanner scan= new java.util.Scanner(System.in);

   //prompts and gets the input from the user
   public void setMake(){
       JOptionPane.showInputDialog(\"Select Buick, Chevrolet, or Pontiac (b,c,p): \");
   char ch = (scan.next()).charAt(0);
   //input validation checking
   while(ch != \'b\' && ch != \'c\' && ch !=\'p\'){
       JOptionPane.showMessageDialog(null,\"The only valid selections are \'b\', \'c\', or \'p\'\");
       JOptionPane.showInputDialog(\"Select Buick, Chevrolet, or Pontiac (b,c,p): \");
   ch = (scan.next()).charAt(0);
   }
     
   switch(ch){
   case \'p\':make=\"Pontiac\"; break;
   case \'b\':make=\"Buick\";break;
   case \'c\':make=\"Chevrolet\";
   }
   }
public void setColor(){
   JOptionPane.showInputDialog(\"Select blue, green, or red (b,g,r): \");
char ch = (scan.next()).charAt(0);

while(ch != \'b\' && ch != \'g\' && ch !=\'r\'){
   JOptionPane.showMessageDialog(null,\"The only valid selections are \'b\', \'g\', or \'r\'\");
   JOptionPane.showInputDialog(\"Select blue, green, or red (b,g,r): \");
ch = (scan.next()).charAt(0);
}

switch(ch){
case \'b\':color=\"blue\"; break;
case \'g\':color=\"green\";break;
case \'r\':color=\"red\";
}
}
//prints the make
public String printMake(){
return make;
}
//prints the color
public String printColor(){
return color;
}
}

//Driver.java

import javax.swing.*;   

public class Driver {
   //main method
   @SuppressWarnings(\"resource\")
       public static void main(String args[]){
   java.util.Scanner keyin=new java.util.Scanner(System.in);
   Automobile auto = new Automobile(); //AutoMobile object

   JOptionPane.showInputDialog(\"How many cars do you want to consider? \");
   int num=keyin.nextInt();
   for(int i=0;i<num;i++){
   auto.setMake();
   auto.setColor();
   JOptionPane.showMessageDialog(null, auto.printColor()+\" \"+auto.printMake());
   }
   }
   }
  

Solution

You should not use Scanner class that is used for getting input from console.

Below are the changes inputDialogBox returns a string and you need to parse that string to a variable.

import java.util.Scanner;
import javax.swing.*;

public class Automobile {
private String make; //Make of the AutoMobile
private String color; //Color of the vehicle
//to read input from the keyboard
private java.util.Scanner scan= new java.util.Scanner(System.in);
//prompts and gets the input from the user
public void setMake(){
String selection = JOptionPane.showInputDialog(\"Select Buick, Chevrolet, or Pontiac (b,c,p): \");
char ch = selection.charAt(0);

//input validation checking
while(ch != \'b\' && ch != \'c\' && ch !=\'p\'){
JOptionPane.showMessageDialog(null,\"The only valid selections are \'b\', \'c\', or \'p\'\");
selection = JOptionPane.showInputDialog(\"Select Buick, Chevrolet, or Pontiac (b,c,p): \");
ch = selection.charAt(0);

}

switch(ch){
case \'p\':make=\"Pontiac\"; break;
case \'b\':make=\"Buick\";break;
case \'c\':make=\"Chevrolet\";
}
}
public void setColor(){
String selection = JOptionPane.showInputDialog(\"Select blue, green, or red (b,g,r): \");
char ch = selection.charAt(0);

while(ch != \'b\' && ch != \'g\' && ch !=\'r\'){
JOptionPane.showMessageDialog(null,\"The only valid selections are \'b\', \'g\', or \'r\'\");
selection = JOptionPane.showInputDialog(\"Select blue, green, or red (b,g,r): \");
ch = selection.charAt(0);

}
switch(ch){
case \'b\':color=\"blue\"; break;
case \'g\':color=\"green\";break;
case \'r\':color=\"red\";
}
}
//prints the make
public String printMake(){
return make;
}
//prints the color
public String printColor(){
return color;
}
}

import javax.swing.*;   
public class Driver {
//main method
@SuppressWarnings(\"resource\")
public static void main(String args[]){
java.util.Scanner keyin=new java.util.Scanner(System.in);
Automobile auto = new Automobile(); //AutoMobile object
int num = Integer.parseInt(JOptionPane.showInputDialog(\"How many cars do you want to consider? \"));
for(int i=0;i<num;i++){
auto.setMake();
auto.setColor();
JOptionPane.showMessageDialog(null, auto.printColor()+\" \"+auto.printMake());
}
}
}

Help! I can get the first GUI to pop up but not anything else. Can you tell me where my code is wrong. import java.util.Scanner; import javax.swing.*; public cl
Help! I can get the first GUI to pop up but not anything else. Can you tell me where my code is wrong. import java.util.Scanner; import javax.swing.*; public cl
Help! I can get the first GUI to pop up but not anything else. Can you tell me where my code is wrong. import java.util.Scanner; import javax.swing.*; public cl

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site