I am trying to catch an exception where text is inserted and the program has to parseInt.
Upon compile it is saying the the variable num might not have been initialised. (Line 31)
I cannot work out why it would be doing this.
Code follows. Thanks in advance.
// Java packages
import javax.swing.JOptionPane;
// program uses JOptionPane
class Help2Gui {
public static void main(String args[]) {
// variable declaration
String choice;
int num;
// read in first number from user as a string
choice =
JOptionPane
.showInputDialog("Help on: \n \t 1. class \n \t 2. object \n \t 3. method \n \t 4. variable \n \t 5. constructor \n \t 6. Quit \n Enter a number from the list above.");
if (choice == null) {
System.exit(0);
}
do { // begin a loop to display initial choice, repeating until 6 is
// entered
// convert numbers from type String to type int
try {
num = Integer.parseInt(choice);
}
catch (NumberFormatException nfe) {
}
switch (num) { // display result for each item entered by user
case 1:
JOptionPane.showMessageDialog(null,
"\n A class is a definition of an object.",
"Java Help System", JOptionPane.PLAIN_MESSAGE);
break;
case 2:
JOptionPane
.showMessageDialog(
null,
"The switch: \n \n switch (expression) { \n case constant: \n statement sequence \n break \n // ... \n } ; ",
"Java Help System", JOptionPane.QUESTION_MESSAGE);
break;
case 3:
JOptionPane
.showMessageDialog(
null,
"The for: \n \n for(init; condition; iteration) \n statement;",
"Java Help System", JOptionPane.INFORMATION_MESSAGE);
break;
case 4:
JOptionPane.showMessageDialog(null,
"The while: \n \n while(condition) statement;",
"Java Help System", JOptionPane.WARNING_MESSAGE);
break;
case 5:
JOptionPane
.showMessageDialog(
null,
"The do-while: \n \n do { \n statement; \n } while (condition);",
"Java Help System", JOptionPane.ERROR_MESSAGE);
break;
case 6:
System.exit(0);
break;
default:
JOptionPane.showMessageDialog(null,
"Enter a number from 1 to 5 or 6 to Quit",
"Java Help System", JOptionPane.ERROR_MESSAGE);
}
// read in first number from user as a string
choice =
JOptionPane
.showInputDialog("Help on: \n \t 1. if \n \t 2. switch \n \t 3. for \n \t 4. while \n \t 5. do-while \n \t 6. Quit \n Enter a number from the list above.");
try {
// attempt to convert the String to an int
num = Integer.parseInt(choice);
}
catch (NumberFormatException nfe) {
JOptionPane.showMessageDialog(null,
"Enter a number from 1 to 5 or 6 to Quit",
"Java Help System", JOptionPane.ERROR_MESSAGE);
}
// convert numbers from type String to type int
}
while (num != 6); // end of do-while loop.
System.exit(0); // terminate application with window
} // end method main
} // end class Help2Gui
If the exception is thrown and caught, the variable
numis not initialized byand then there will be a problem with:
To solve this – you can give a default value for these cases (in the exception handler, or prior to the try block, so the assignment to
numwill override the default value), or terminate the method when the exception is thrown.Simplest solution IMHO (though it not always fits) will be to initialize
numwhile declaring it, something like: