• 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

How to verify if value is numeric

 
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Test that with "abc" and I think you'll find the exception the earlier posts were talking about.
 
reply
    Bookmark Topic Watch Topic
  • New Topic