aspose file tools*
The moose likes Beginning Java and the fly likes NumberFormatException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "NumberFormatException" Watch "NumberFormatException" New topic
Author

NumberFormatException

Flo Powers
Ranch Hand

Joined: May 12, 2004
Posts: 57
Hi, I am doing input from the keyboard and want to safeguard against accidental pressing of the <enter> key without any value having been entered first. I have some simple code here (for demo purposes), and tried to check when it crashes, but don't see a pattern yet (or rather, I see it, but I don't understand it).

This, for example, does not crash, even if I enter nothing before pressing <enter>:



On the other hand, this will give a NumberFormatException:



In other words (and as indicated by the name of the exception), the problem occurs when I try to convert to a number. An empty line is not null then? I doesn't seem to work with using double quotation marks either (empty string).I'm not sure how I should be checking for empty input in the conditional statement...

Thanks for any helpful comments!
Flo

[ January 25, 2005: Message edited by: Flo Powers ]
[ January 25, 2005: Message edited by: Flo Powers ]
Flo Powers
Ranch Hand

Joined: May 12, 2004
Posts: 57
Hmm, I guess this might be the answer...



If there are better ways, please let me know.

Thanks,
Flo
ramprasad madathil
Ranch Hand

Joined: Jan 24, 2005
Posts: 489

yes, that would be the way to go.
a small thought - if u would wish to accept numbers with decimal places, consider using

else if user enters 10.01, it would report "You did not enter a number" :-)

TX,
ram.
Flo Powers
Ranch Hand

Joined: May 12, 2004
Posts: 57
Thank you, that makes a lot of sense, of course.
Flo
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: NumberFormatException