my dog learned polymorphism
The moose likes Java in General and the fly likes How to verify if value is numeric Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to verify if value is numeric" Watch "How to verify if value is numeric" New topic

How to verify if value is numeric

bob connolly
Ranch Hand

Joined: Mar 10, 2004
Posts: 204

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?

Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
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 ]

Joined: Nov 24, 2005
Posts: 4
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

Joined: Aug 19, 2006
Posts: 42
This is the code.

string s="345"
s.o.p("this is integer");

Sudhakar Reddy
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Test that with "abc" and I think you'll find the exception the earlier posts were talking about.

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
I agree. Here's the link:
subject: How to verify if value is numeric
jQuery in Action, 3rd edition