• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

NumberFormatException

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmm, I guess this might be the answer...



If there are better ways, please let me know.

Thanks,
Flo
 
Ranch Hand
Posts: 489
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you, that makes a lot of sense, of course.
Flo
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic