简体   繁体   中英

Check if a string contains only a set of letters

I'm trying to check if a word contains only a set of letters such as I,O,S,H and X Suppose the user enters: SSHX, the output will be yes but if the user enters SHEXX, the output will be NO

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);

    String word = sc.next();
    word = word.toUpperCase();

    int length = word.length();
    char letter = 0;

    for (int counter = 0; counter < length; counter++) {
        letter = word.charAt(counter);
    }
    if (letter != 'I' && letter != 'O' && letter != 'S' && letter != 'H' && letter != 'Z' && letter != 'X' && letter != 'N') {
        System.out.print("NO");
    } else {
        System.out.print("YES");
    }
}

You have a good way of solving it. The problem is that you're not actually checking each letter, so you need to do the checks inside of the for loop or you will only check the last letter. But then you can't print the "YES" as you only want to print it if ALL letters are yes, so you can use a boolean value to check that as well, as such:

    boolean isMatch = true; 
    for (int counter = 0; counter < strLength && isMatch; counter++) {
        letter = word.charAt(counter);
        if (letter != 'I' && letter != 'O' && letter != 'S' && letter != 'H' && letter != 'Z' && letter != 'X' && letter != 'N') {
            System.out.print("NO");
            isMatch = false;
        } 
    }
    if (isMatch) {
        System.out.print("YES");
    }

But, as others have pointed out using a regular expression is more effective (and this one has a working regex for what you want.. The asterisk means zero or more of what's inside the brackets. ):

    if (word.matches("[HIOSX]*")) {
        System.out.print("YES");
    } else {
        System.out.print("NO");
    }

Use a regular expression .

String regex = "[OSXHI]*";
String string = "SOMETHING";
Matcher matcher = Pattern.compile(regex).matcher(string);
if (matcher.find())
{
    String match = matcher.group(1);
    System.out.println(match);
}

Some additional resources:

Aside from the obvious answer of using a regular expression, consider using Google's Guava API to make this pretty simple:

if(CharMatcher.anyOf("HIOSX").matchesAllOf(word)) { 

} ...

Use a regular expression:

if (word.matches("[HIOSX]+"))
    System.out.println("YES");
else
    System.out.println("NO");

first of all you should initialize letter like this: char letter = '0'; instead of 0 second of all your for loop is badly used try this code:

 boolean isInSet;
    for (int counter = 0; counter < strLength; counter++) 
    {
        letter = word.charAt(counter);
        if (letter != 'I' && letter != 'O' && letter != 'S' && letter != 'H' && letter != 'Z' && letter != 'X' && letter != 'N') 
        {
            isInSet=false;
            counter=strlength; //end loop
        } 
        else 
        {
           isInSet=true;
        }
    }
    if(isInSet=true)
    {
       System.out.print("YES");
    }
    else
    {
       System.out.print("NO");
    }

Now the loop will loop over the string and check if each character is in the set, if it isnt the loop ends and the boolean is set to false which results in the NO output

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM