• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

String to number

 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi there,
I would like to check if the string i got is a valid number.
for example a "abc" will false.
"123" will return true.
I have tried new Integer("abc"); but I get a NumberFormatException.
Is there another way to check that the string is a valid number without getting exceptions?
thanks heaps,
sarone
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Write a short function which calls Integer.parseInt() inside a try block. If it succeeds, the routine should return "true"; from the catch block, it should return "false". That's the best you're going to do short of writing your own little number-parsing routine.
 
Ranch Hand
Posts: 1140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or use Double.parseDouble() method to handle floating numbers and large numbers.
 
Ranch Hand
Posts: 577
Tomcat Server Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ernest and Mani gave nice solutions and I hereby give u one more solution which comes from basic understanding.
If the string does contain all digits then it will return true. Use ASCII code to write a If block to perform this.
 
Mani Ram
Ranch Hand
Posts: 1140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Naren Chivukula:
If the string does contain all digits then it will return true. Use ASCII code to write a If block to perform this.


And write additional code to handle unary minus & decimal points (like there can be atmost one decimal point & one minus sign, and if the minus sign is present, it should be the first character of the string etc), if required.
 
Naren Chivukula
Ranch Hand
Posts: 577
Tomcat Server Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mani has pointed out correctly. Check unary plus/unary minus/decimal point.
If you are expecting binary/octal/hexal. check the string accordingly.
 
Ranch Hand
Posts: 382
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Naren Chivukula:
Ernest and Mani gave nice solutions and I hereby give u one more solution which comes from basic understanding.
If the string does contain all digits then it will return true. Use ASCII code to write a If block to perform this.


Why would you want to do this when you can convert the string into a char[] & Character has isDigit method?
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As you can see, there IS a way to avoid exceptions altogether. Personally, I like using the Integer.parseInt() method and dealing with the exception that is thrown. This usually takes fewer lines of code.
Of course, doing it the "old fashioned way" is a good academic exercise, if nothing else.
Layne
[ February 20, 2004: Message edited by: Layne Lund ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic