• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String to number

 
Sarone Thach
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • 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
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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.
 
Mani Ram
Ranch Hand
Posts: 1140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or use Double.parseDouble() method to handle floating numbers and large numbers.
 
Naren Chivukula
Ranch Hand
Posts: 577
Java Notepad Tomcat Server
  • Mark post as helpful
  • send pies
  • 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
  • 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
Java Notepad Tomcat Server
  • Mark post as helpful
  • send pies
  • 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.
 
Sadanand Murthy
Ranch Hand
Posts: 382
  • Mark post as helpful
  • send pies
  • 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?
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • 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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic