• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

Pattern Match

 
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!
 
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.
 
author
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
 
Wink, wink, nudge, nudge, say no more, it's a tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!