This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

is there anything like isNum() ?

 
Mandar Gandhe
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 9258
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could iterate through the String and check each character with Character.isDigit().
 
Wayne L Johnson
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanx Wayne L Johnson,
I got really valuable knowledge from u also....
 
Mandar Gandhe
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much for the information !!!
Mandar Gandhe
 
Brian Pipa
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
anks garg
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 399
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Greg Reinl
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic