Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check that a string contains characters other than those specified. (in Java) [closed]

Tags:

java

string

regex

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)
like image 946
Jose Avatar asked Mar 01 '13 03:03

Jose


1 Answers

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");
}
like image 168
Indigenuity Avatar answered Sep 18 '22 12:09

Indigenuity