• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to verify if value is numeric

 
bob connolly
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

If i have a string that contains a numeric value ie '544' for instance, is there a method that can tell me if the value is numeric?

thanks!
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Define "numeric". Is 0xA80 numeric? What about FF or 909.09? Generally speaking if you're talking about an integral value in base 10 you could use String.matches("-?\\d+") (I think) or you could try passing it to Integer.parseInt() and catching the exception if it occurs. If it's more complicated than that then you'll have to get more specific I think.
[ October 27, 2006: Message edited by: Ken Blair ]
 
BobC
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ken, going to try that parseInt, sounds like a good idea, don't mind if it exceptions out!

Have a nice weekend Ken!
 
Sudhakar Reddy Kurakula
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
This is the code.

string s="345"
if(Integer.parseInt(s)>=0)
s.o.p("this is integer");
else
s.o.p("not");

Cheers
Sudhakar Reddy
M.C.A(2006)
Satyam.
9989223696
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Test that with "abc" and I think you'll find the exception the earlier posts were talking about.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic