aspose file tools*
The moose likes Java in General and the fly likes is there anything like isNum() ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "is there anything like isNum() ?" Watch "is there anything like isNum() ?" New topic
Author

is there anything like isNum() ?

Mandar Gandhe
Greenhorn

Joined: Aug 01, 2002
Posts: 29
Hi..
In my application, I receive a string which sometimes can contain all the digits ("123137414") or sometimes contains some alphabets too ("jsajh2627517). I need to do separate processing depending upon whether this string is a number or not.
I can do this using Integer class and then checking for exceptions, but I am looking for a method without generating any exceptions. So is there any method something like
boolean isNum(String)
Thanks and Regards
Mandar
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8927
    
    9

You could iterate through the String and check each character with Character.isDigit().


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Wayne L Johnson
Ranch Hand

Joined: Sep 03, 2003
Posts: 399
In JDK 1.4.x you can use the pattern matching capabilities of regular expressions:

The pattern is \d+ which means one or more digits. You can do a shortened version,
boolean b = Pattern.matches("\\d+", "target-string");, but the above method allows the pattern to be pre-compiled and will save you some time if you have to do lots of matches.
Check out the documentation at java.util.regex.Pattern API documentation.
Ko Ko Naing
Ranch Hand

Joined: Jun 08, 2002
Posts: 3178
Thanx Wayne L Johnson,
I got really valuable knowledge from u also....


Co-author of SCMAD Exam Guide, Author of JMADPlus
SCJP1.2, CCNA, SCWCD1.4, SCBCD1.3, SCMAD1.0, SCJA1.0, SCJP6.0
Mandar Gandhe
Greenhorn

Joined: Aug 01, 2002
Posts: 29
Thank you very much for the information !!!
Mandar Gandhe
Brian Pipa
Ranch Hand

Joined: Sep 29, 2003
Posts: 299
what's wrong with using Integer and catching the exception? It's simple, the code is easy to read, and it works. Just curious what the aversion is to it.
Brian


My Java Freeware:<br />MACCC - <a href="http://maccc.pipasoft.com" target="_blank" rel="nofollow">http://maccc.pipasoft.com</a><br />Nator - <a href="http://nator.pipasoft.com" target="_blank" rel="nofollow">http://nator.pipasoft.com</a>
anks garg
Greenhorn

Joined: Dec 15, 2003
Posts: 2
you can simply use
if (myString.matches("\\d{15}") == false){
.....
}
where myString.matches will return true if myString contains only numerals and exactly 15 numerals. If you do not want to fix the length , just use -
myString.matches("\\d")
hope this helps .
Wayne L Johnson
Ranch Hand

Joined: Sep 03, 2003
Posts: 399
These responses show that there are always many ways of doing things in Java, and choosing one method over another may be a matter of preference.
I ran a set of [very unscientific] tests and found that the four solutions, in order of performance, is:
1) Loop through string, calling "Character.isDigit()"
2) Pre-compiled Pattern,
3) String.match(...);
4) Integer.parseInt();
However the variance was no more than 2-2.5 seconds between best and worst when checking 1,000,000 numbers.
So using the "Integer.parseInt()" method and checking for an exception is acceptable. In the case where you not only want to test if the String represents a number, but you also want to get the "int" value, this would probably be the best solution.
Looping through and checking for all digits was the fastest. But you don't get the value, and it isn't very extensible.
Using regular expressions won't give you the "int" value either, but it's faster than doing "Integer.parseInt()" [though the time savings is probably negligable]. However you do get some advantages. First, you learn a new Java API, and I think that learning new things is good. Second, it's very extensible, in that if you later need to check for a different pattern you can easily modify the code to handle it.
Let's say you need to check to see if something is a valid credit card number. You need to ensure it has exactly 16 digits, with optional spaces or hyphens between each set of four digits. You can forget doing the "Integer.parseInt()", or looping through. Now regular expressions come in real handy. You just have to decide if you want to pre-compile the pattern, or do a "Pattern.matches(...,...)" or "String.matches(...)" every time.
I realize that these comments are well beyond the scope of the original question, but I think it can be valuable to examine the strengths and weaknesses of each solution, and then apply the one that best fits the problem.
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1821

Originally posted by Brian Pipa:
what's wrong with using Integer and catching the exception? It's simple, the code is easy to read, and it works. Just curious what the aversion is to it.
Brian

If you're doing heavy text-file-to-number processing, the performance of throwing and catching exceptions really starts to add up. I ran a quick test which showed that exceptions took nearly 70 times longer to parse bad strings than the char-by-char isDigit method, especially when the strings were 7 characters long or higher. This is a place where you should worry about the perofmrnace of the chosen method, especially if you are trying to write some intensive math program. (If you're just parsing 1 or two strings, it probably doesn't really matter what method you choose.)
As far as readibility, I agree that the readibility of the isDigit method is somewhat less than the Exception, but it is not that much less and a well-placed comment explaining what was done any why can help with that


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

Joined: Feb 11, 2003
Posts: 45
what's wrong with using Integer and catching the exception? It's simple, the code is easy to read, and it works. Just curious what the aversion is to it.

I would have an aversion to it because I prefer to reserve exceptions for exceptional conditions. The original post implied that it is expected that the String might contain alpha characters and if so the application does some different processing than if the String contains all digits. So in the context of this application I wouldn't consider alphas in the String to be an exceptional condition.
 
wood burning stoves
 
subject: is there anything like isNum() ?