On Jul 23, 8:10 am, ben <[email protected]> wrote:
> hi guys!!!
>
> i need help in fixing the following errors...
>
> Exception in thread "main" java.lang.NumberFormatException: For input
> string: " "
> at java.lang.NumberFormatException.forInputString
> (NumberFormatException.java:48)
> at java.lang.Integer.parseInt(Integer.java:449)
> at java.lang.Integer.parseInt(Integer.java:499)
> at InputFromKeyboardJOptionPane.main
> (InputFromKeyboardJOptionPane.java:20)
> Java Result: 1
> BUILD SUCCESSFUL (total time: 0 seconds)
You need probably to englobe your call to JOptionPane inside a try
catch block, and catch the NumberFormatException. All of this inside a
do while loop to loop over the JOptionPane till the user enters a
valid age.
Something like that will do it:
// Ask for an age, that is something positive
do {
try {
age = Integer.parseInt(JOptionPane.showInputDialog
(null,
"Please
enter your age",
app,
JOptionPane.QUESTION_MESSAGE));
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(null,
"Please
enter a positive number",
app,
JOptionPane.ERROR_MESSAGE);
} catch (HeadlessException ex) {
ex.printStackTrace();
}
} while (age<=0);
Well, this one makes a little more than you want.
--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/javaprogrammingwithpassion?hl=en
-~----------~----~----~----~------~----~------~--~---