Here is a program I have to write for an assignment. I cannot get it to exit when I click cancel and for some reason I get an error when you enter 3 as your option. I am sure it is a beginners mistake but I am stuck. Any assistance would be greatly appreciated. Just be easy...I am still new. Thanks.
*************** *************** *************** *************** *********
*************** *************** *************** *************** *********
Code:
import java.io.*;
import javax.swing.JOptionPane;
import java.text.DecimalFormat;
public class MyType1
{
public static void main (String[] args)
{
//Declaring Class Variables
String strChoice, strTryString, strTryInt, strTryDouble;
int choice, tryInt;
double tryDouble;
boolean done = false;
while (!done)
//while (!done= null)
{
strChoice = JOptionPane.showInputDialog (null,"What's My Type?\n\n1 ) String\n2 ) integer\n3 ) double\n4 ) Quit the program");
strTryDouble = (null);
//strTryDouble = dataIn.readLine();
try
{
choice = Integer.parseInt (strChoice);
//if (choice == null) finish ();
if (choice == 4) exit ();
switch (choice)
{
case 1:
JOptionPane.showMessageDialog (null, "Correct!!", "THAT’S CORRECT!!!", JOptionPane.INFORMATION_MESSAGE);
break;
case 2:
tryInt = Integer.parseInt (strChoice);
JOptionPane.showMessageDialog (null, "Correct!!", "THAT’S CORRECT!!!", JOptionPane.INFORMATION_MESSAGE);
break;
case 3:
tryDouble = Double.parseDouble (strTryDouble);
JOptionPane.showMessageDialog (null, "Correct!!", "THAT’S CORRECT!!!", JOptionPane.INFORMATION_MESSAGE);
break;
case 4:
default:
throw new NumberFormatException();
//break;
}
}
catch (NumberFormatException e)
{
JOptionPane.showMessageDialog (null, "Wrong!!", "WAAAY OFF!!!", JOptionPane.INFORMATION_MESSAGE);
}
}
exit ();
}
public static void exit()
{
System.exit(0);
}
}
Comment