It's not a secret anymore!
The moose likes Java in General and the fly likes Pattern Match Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Pattern Match" Watch "Pattern Match" New topic

Pattern Match

Paul Keohan
Ranch Hand

Joined: Mar 15, 2000
Posts: 411
Is there a quick way to check for a String containing numerics? Rather than going through each individual number (0 - 9) and seeing if it's contained within the String, I'd like to be able to return a true or false with one check for whether numerics are contained ot not.
Paul Stevens
Ranch Hand

Joined: May 17, 2001
Posts: 2823
Integer.parseInt(string) will throw a NumberFormatException if not all numbers so just catch and return false else return true.
Peter den Haan
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
I assume that you want to know if the string contains any numbers, and not just (as assumed by the previous respondent) if it is a valid number. Java 1.4 will have a regular expression library in the platform (YES! ). In the mean time, you could use the one from the Apache project - Oro. However, if all you want is something quick and dirtyThis should do the job (haven't tested it though). Sufficiently fast for most purposes and just a single line of code. However, I would suggest leaving it in a method of its own as shown above as it functionality is not immediately obvious.
- Peter
I agree. Here's the link:
subject: Pattern Match
It's not a secret anymore!