• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

NumberFormatException

 
Flo Powers
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm, I guess this might be the answer...



If there are better ways, please let me know.

Thanks,
Flo
 
ramprasad madathil
Ranch Hand
Posts: 489
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, that makes a lot of sense, of course.
Flo
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic