Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Exception Handling: String entered instead of an Integer

807600Nov 3 2007 — edited Nov 3 2007
Hi. 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....
}

}
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 1 2007
Added on Nov 3 2007
3 comments
2,618 views