File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes String - IsNumeric()? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "String - IsNumeric()?" Watch "String - IsNumeric()?" New topic
Author

String - IsNumeric()?

Lambert Stein
Greenhorn

Joined: Oct 30, 2006
Posts: 14
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

Joined: Oct 09, 2006
Posts: 94
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

Joined: Jan 10, 2002
Posts: 61316
    
  66

"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


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
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
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
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

Joined: Oct 30, 2006
Posts: 14
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

Joined: Aug 16, 2005
Posts: 14196
    
  20

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18876
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18876
    
  40

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

Joined: Jan 30, 2000
Posts: 18671
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

Joined: Oct 30, 2006
Posts: 14
Thanks a lot, everyone. I really appreciate it.
Steve Palace
Greenhorn

Joined: Aug 17, 2012
Posts: 1
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

Joined: Oct 13, 2005
Posts: 39100
    
  23
Welcome to the Ranch

That looks useful, but I wish Apache had given some better documentation to that class.
 
GeeCON Prague 2014
 
subject: String - IsNumeric()?