• 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
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

how to avoid NumberFormatException

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As we know, while parsing a string in to int/floal by using the method Integer.parseInt(String s)/Float.parseFloat(String s), it will return int/float value, if the String contains any numeric value like "101" or "101.11". but if it is not numeric like "a12", then it will throw NumberFormatException.

Please let me know, is there way to avoid this Exception except handling the exception means try catch block.

Thanks in advance.
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Personally I check the string first with a regular expression. But you could also split the sting in to a character array and check each character is acceptable in an number.
 
Ranch Hand
Posts: 116
Eclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could just let it happen and then catch the exception, i.e.



Personally I find that easier than doing a validity check beforehand.
What to do if the string is not a valid numeric is more an issue and that's app specific.
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To avoid this exception you have to feed a valid integer or float values to parsers... this is the idea only... as you can not peal coconuts using potato pealers.. so to avoid breakage (of pealer) use potato only... not the coconut.. otherwise.. just check before pealing.. whether you are trying with potato only or not.... simple..
 
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why dont you want to make use of try catch ? Java provides very robust exception handling mechanism.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JDK 5 provided another nice alternative:
 
yeah, but ... what would PIE do? Especially concerning this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic