Exception Handling: String entered instead of an Integer
807600Nov 3 2007 — edited Nov 3 2007Hi. I am new to Java and exception handling. I have the following program and need corrections to allow it to work.
This is what I want:
1) The user to enter a number
2) Display the number, but if the number is not a number , eg 's',
3) then the program must ask the user to re-enter the number until an actual integer is entered.
_____________________________
import javax.swing.JOptionPane;
public class ExceptionHandling
{
public static void main (String[] args)
{
int num = 0;
try
{
String input = JOptionPane.showInputDialog("Please enter a number: ");
num = Integer.parseInt(input);
System.out.println("The number you entered is: " + num);
}
catch(Exception e)
{
// do not know what code to put here to make it return to the try....
}
}
}