aspose file tools*
The moose likes Beginning Java and the fly likes is String number? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "is String number?" Watch "is String number?" New topic
Author

is String number?

Pavel Kubal
Ranch Hand

Joined: Mar 13, 2004
Posts: 356
How to check if given String variable is number?
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
There are a couple approaches.
1) You can look at the string and see if it looks like a number. Regular expressions could match digits and decimal points and signs and all those things. If you're pre JDK 1.4 and don't have regular expressions, you can inspect each character. You'd pretty much have to duplicate all the syntax rules of Java numbers to handle all numeric types.
2) Use one of the numeric types to parse the String, like Integer.parseInt(String). If you feed it non numeric data, or data out of range or otherwise unsuitable to the type, you'll get an exception to let you know. You can do this in a few lines without knowing the syntax and ranges and so on, because the JDK will do all the checking for you.
With either approach, consider creating a utility class to house the logic. "Utility" here is an unofficial OO term for classes with all static methods. You'll be able to use it like this for years to come:


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1817

Just as a note, option #1 is more efficient that option #2, which can take a thousand times as long to determine that a string is not a number. When your talking one option taking 100 milliseconds instead of .1 millisecond, your gain is not much from a human perspective. When your doing 100,000 of these checks, however, the time can add up (10,000 seconds as opposed to 10 seconds).


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Pavel Kubal
Ranch Hand

Joined: Mar 13, 2004
Posts: 356
..and how can I do it without any exception, which can stop executing my program?
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Option 1 uses no exceptions. Option 2 throws an exception, but you can catch it, and prevent it from stopping your program. Since this is posted in Beginner, I suspect that option 2 is best for you - you need to study how to use try/catch loops to catch the exception, as this is a standard technique of Java programming. Option 1 is a more advanced technique, in my opinion.


"I'm not back." - Bill Harding, Twister
Pavel Kubal
Ranch Hand

Joined: Mar 13, 2004
Posts: 356
Good, can you post few lines of code of option 1? I'm not sure how can I do it. Thanks in advance.
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
If you're interested to explore the regular expression approach, then I'd suggest you might like to read an introductory article to the subject that I wrote.
If you're interested to create a solution without the use of the java.util.regex API, then I'd suggest taking a look at the J2SE API Documentation for some of the methods in the String and Characters classes. In the String class, you might make use of the charAt method. In the Character class, you might find something that helps to determine if a particular character is a digit.
Get started, and folks around here will be happy to nudge you along in the right direction.


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
There's kind of a 3rd approach I used in COBOL (!) in the previous century. It's a bit of pattern matching plus a bit of converting from character to number.

When done, you have either a number (say an int) or an error message, never both. I left to your imagination how to convert a char to a number 0-9. Hint: it's easy. You'd want some range checking to make sure you don't exceed Integer.MAX_VALUE or MIN_VALUE.
For fancy datatypes that have decimal points, floating bits, etc things get harder. RegEx is a neater way to check syntax for those.
Max Habibi
town drunk
( and author)
Sheriff

Joined: Jun 27, 2002
Posts: 4118

HTH,
M
[ April 20, 2004: Message edited by: Max Habibi ]

Java Regular Expressions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: is String number?