View Single Post
  #2 (permalink)  
Old 08-14-2007, 04:49 PM
shanePreater shanePreater is offline
Member
 
Join Date: Jul 2007
Location: England, Bath
Posts: 47
shanePreater is on a distinguished road
I would recommend you use a couple of regex functions to test this. That would probably be the easiest way of doing it:
Code:
String numbers = "0123456789"; String letters = "abcde"; String rubbish="$%^&£\""; Pattern numberPattern = Pattern.compile("^[0-9]+$"); Pattern letterPattern = Pattern.compile("^[a-zA-Z]+$"); System.out.print("Numbers should match number pattern: "); System.out.println(numberPattern.matcher(numbers).matches()); System.out.print("Numbers should not match letter pattern: "); System.out.println(letterPattern.matcher(numbers).matches()); System.out.print("Letters should match letter pattern: "); System.out.println(letterPattern.matcher(letters).matches()); System.out.print("Letters should not match number pattern: "); System.out.println(numberPattern.matcher(letters).matches()); System.out.print("Rubbish should not match number pattern: "); System.out.println(numberPattern.matcher(rubbish).matches()); System.out.print("Rubbish should not match letter pattern: "); System.out.println(letterPattern.matcher(rubbish).matches());
I would recommend making the Patterns private static final in your class so they are only compiled once this will make it pretty efficient.

Hope that helps,
Shane.
__________________
Shane Preater -
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Reply With Quote