Hi I have some problems with an error msg saying
ava:37: variable choice might not have been initialized
switch(choice)
I was hoping that someone could tell me the error I mean whats really causing it. I saw that someone else had the same problem but it didnt help and I was just hoping someone might have a some advice for me.
CODE
import java.io.*;
import javax.swing.JOptionPane;
public class MyType
{
public static void main(String[] args)
{
//declaring variables
String strChoice = " ", strTryString, strTryInt, strTryDouble;
int choice, tryInt;
double tryDouble;
boolean done = false;
double A;
//loop while not done
while (!done)
{
try
{
String message = "What is My Type?:" + "\n\n1)String\n2)Integer\n3)double\n4)Quit the program\n\n";
choice = Integer.parseInt(strChoice);
//test for valid choice 1, 2, 3, or 4
if (choice<1 || choice>4) throw new NumberFormatException();
else done = true;
}
catch (NumberFormatException e)
{
JOptionPane.showMessageDialog(null, "Please enter a 1, 2, 3 or 4:", "Error", JOptionPane.INFORMATION_MESSAGE);
switch(choice)
{
case 1:
JOptionPane.showInputDialog(null,"Any Letter Is A String");
break;
case 2:
JOptionPane.showMessageDialog(null, "Correct!");
tryInt = Integer.parseInt(strChoice);
break;
case 3:
JOptionPane.showMessageDialog(null, "Correct!");
tryDouble = Integer.parseInt(strChoice);
break;
case 4:
done = true; JOptionPane.showMessageDialog(null,"Closing!");
System.exit(0);
break;
default: throw new NumberFormatException();
}
}
{
JOptionPane.showMessageDialog(null, "Invalid, try again.");
}
}
}
}
~edit: code tags added PBThis post has been edited by PennyBoki: 4 Feb, 2008 - 05:30 PM