• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String - IsNumeric()?

 
Lambert Stein
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • 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!
 
Nick White
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • 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!
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65120
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • 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?
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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).
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15441
41
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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.
 
Henry Wong
author
Marshal
Pie
Posts: 21417
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • 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
Marshal
Pie
Posts: 21417
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thanks a lot, everyone. I really appreciate it.
 
Steve Palace
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • 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).
 
Campbell Ritchie
Sheriff
Pie
Posts: 49796
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

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