简体   繁体   English

检查字符串是否只包含一组字母

[英]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 我试图检查一个单词是否只包含一组字母,如I,O,S,H和X假设用户输入:SSHX,输出将为yes但如果用户输入SHEXX,输出将为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. 问题是你实际上没有检查每个字母,所以你需要在for循环中进行检查,否则你只会检查最后一个字母。 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: 但是你不能打印“YES”,因为如果所有字母都是肯定的,你只想打印它,所以你也可以使用布尔值来检查它,如下:

    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: 除了使用正则表达式的明显答案之外,请考虑使用Google的Guava API来实现这一点:

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'; 首先你应该像这样初始化字母: char letter = '0'; instead of 0 second of all your for loop is badly used try this code: 而不是0秒所有你的for循环使用不当尝试此代码:

 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 现在循环将循环遍历字符串并检查每个字符是否在集合中,如果循环结束并且布尔值设置为false,则导致NO输出

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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