• 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 - IsNumeric()?

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm new here... but I'm a computer freak (more than literally ).
I am great at VB.NET... but I have a function which does not have a corresponding one in java: IsNumeric(String input).
Does anyone know how to find out whether a String is a number (int or floating-point)?
Thanks.
P.S.: Please do not tell me about Character.IsDigit - I don't want loops!
 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Naming Convention

I don't know, but what I do know is that you will probably not get any responses unless you have read the link!

Welcome by the way!
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"AMD Turion",

There aren't many rules that you need to worry about here on the Ranch, but one that we take very seriously regards the use of proper names. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it.

In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

Thanks!
bear
JavaRanch Sheriff
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

P.S.: Please do not tell me about Character.IsDigit - I don't want loops!

Whats the matter with loops. Any solution given will probably (whether expilcitly or internally) have to loop through each character in the string and see if its a digit or a '.' (although not more than one). What kind of solution were you interested in?
 
Ranch Hand
Posts: 4632
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you could probably write your own, something like this



don't know about vb.net, but in vb6 isNumeric allowed $ and a few other characters,
so you might not get exactly what you're used to.
 
Lambert Stein
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh, OK. Thanks everyone...
Also, I don't want to write a loop myself; I want it to be done by the computer. (no great reason, but I just want to. Not that I can't).
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello AMD (please change your name, see Nick's and Bear's replies above),

I don't understand why a loop would be such a problem.

But if you absolutely don't want to use a loop, you could do it using a regular expression

Lookup the API documentation of class Pattern in the package java.util.regex to understand how it works.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jesper Young:
But if you absolutely don't want to use a loop, you could do it using a regular expression

Lookup the API documentation of class Pattern in the package java.util.regex to understand how it works.



Or you can use the convenience method available from the String class...



Henry
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Note that the original question allowed for floating-point as well. Ignoring the possibility of exponential notation, this suggests a more elaborate regex like:

Unfortunately that's a bit involved for the beginner forum. So, Lambert: if you are familiar with regular expressions, or are sufficiently motiviated to learn more about them, I recommend something like the above. (Note that I haven't tested it, so don't be too trusting that everything there is correct.) If not, well, the try/catch with Double.parseDouble() that Garrett suggested is the simplest thing to code.
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I forgot who showed me this... so apologies...

Here is a more optimized regex for detecting floating point numbers -- no need for grouping or the OR operator.



Henry
[ October 31, 2006: Message edited by: Henry Wong ]
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah - though that will reject something like "42.", which may or may not be something the user might expect to be able to enter. Such a format is legal for a floating-point literal in Java, after all. Though most people would use "42" or "42.0" instead.
 
Lambert Stein
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot, everyone. I really appreciate it.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know that I am late to party here, but here is some more info on this topic, solving the numeric check by using Apache Commons, more info here: Stack Overflow (http://stackoverflow.com/a/3507236/549510).
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

That looks useful, but I wish Apache had given some better documentation to that class.
reply
    Bookmark Topic Watch Topic
  • New Topic