I have a program that asks the user to input a three character string. The string can only be a combination of a, b, or c.
How do I check if the string contains any other characters than those specified without doing a million conditional statements.
Pseudo example:
String s = "abq"
if (s.containsOtherCharacterThan(a,b,c))
System.exit(-1)
To look for characters that are NOT a, b, or c, use something like the following:
if(!s.matches("[abc]+"))
{
System.out.println("The string you entered has some incorrect characters");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With