Win a copy of Head First Android this week in the Android forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

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
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic