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!
"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.
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?
Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
System.out.println(isNumeric("123"));
System.out.println(isNumeric("123.45"));
System.out.println(isNumeric("$123"));
System.out.println(isNumeric("123x"));
public static boolean isNumeric(String str)
double d = Double.parseDouble(str);
catch(NumberFormatException nfe)
return false;
return true;
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.
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.
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.
"I'm not back." - Bill Harding,
Twister
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 ]
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.
"I'm not back." - Bill Harding,
Twister
current ranch time (not your local time) is
Jun 13, 2024 09:30:43