Hi.. i need to validate two different Strings.
The first String has to only be characters and no numbers
The second String has to be only numbers and no characters. How would I go about doing this?
Many thanks
Printable View
Hi.. i need to validate two different Strings.
The first String has to only be characters and no numbers
The second String has to be only numbers and no characters. How would I go about doing this?
Many thanks
I would recommend you use a couple of regex functions to test this. That would probably be the easiest way of doing it:
I would recommend making the Patterns private static final in your class so they are only compiled once this will make it pretty efficient.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());
Hope that helps,
Shane.
Thanks for the reply... im not too sure though how to use your code..
im learning java at the moment and not too advanced.. learning interface class at the moment.
thanks
OK I'll put it into context a bit more then:
This will allow you to simply test your strings against the methods then in your main method you can do: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();
}
}
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.Code:public class Main {
/**
* @param args
*/
public static void main(String[] args) {
String numberString = "123";
StringValidator.isAllNumbers(numberString);
String letterString = "abc";
StringValidator.isAllLetters(letterString);
}
}
If you need more help give me a shout.
Shane.