View Single Post
  #4 (permalink)  
Old 08-14-2007, 06:34 PM
shanePreater shanePreater is offline
Member
 
Join Date: Jul 2007
Location: England, Bath
Posts: 47
shanePreater is on a distinguished road
OK I'll put it into context a bit more then:
Code:
import java.util.regex.Pattern; /** * Validates String objects. * * @author Shane Preater */ public class StringValidator { private static final Pattern numberPattern = Pattern.compile("^[0-9]+$"); private static final Pattern letterPattern = Pattern.compile("^[a-zA-Z]+$"); /** * Tests the given string contains only numbers. * * @param string * the string to test * @return <code>true</code> is it only contains numbers. */ public static boolean isAllNumbers(String string) { return numberPattern.matcher(string).matches(); } /** * Tests the given string contains only letters. * * @param string * the string to test * @return <code>true</code> is it only contains letters. */ public static boolean isAllLetters(String string) { return letterPattern.matcher(string).matches(); } }
This will allow you to simply test your strings against the methods then in your main method you can do:
Code:
public class Main { /** * @param args */ public static void main(String[] args) { String numberString = "123"; StringValidator.isAllNumbers(numberString); String letterString = "abc"; StringValidator.isAllLetters(letterString); } }
Obviously you will need to edit the code in the Main class to get the strings from where ever you are expecting them and then call the methods as per the example.

If you need more help give me a shout.
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