• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Pattern Match

 
Paul Keohan
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Thanks!
 
Paul Stevens
Ranch Hand
Posts: 2823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Integer.parseInt(string) will throw a NumberFormatException if not all numbers so just catch and return false else return true.
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic