简体   繁体   English

如何检查包含字符串的数组中是否有另一个字符串数组中的某些单词

[英]How to check if an array containing strings, has certain words in it from another string array

I have a input string array (called inputArray_1) of length 3. I have another string array (called keywords) with keywords like so: 我有一个长度为3的输入字符串数组(称为inputArray_1)。我还有另一个具有以下关键字的字符串数组(称为关键字):

 System.out.println("Input 1:");

 for (int i = 0; i < inputArray_1.length; i++)
 {
     inputArray_1[i] = reader.nextLine();
 }


String[] keywords = {"dog", "cat", "monkey", "apple"};

The output of inputArray_1 might look like this: inputArray_1的输出可能如下所示:

The dog had many friends.

So I want to check each index of the array, and see if the string's at each index has any of the keywords in it. 因此,我想检查数组的每个索引,并查看每个索引处的字符串是否在其中包含任何关键字。 It doesn't have to include all the keywords just one of them. 它不必只包含所有关键字之一。

So far I have this and it is not giving me the right input. 到目前为止,我已经掌握了这个功能,但并没有给我正确的输入。

for(int i = 0; i < inputArray_1.length; i++)
        {

            if(inputArray_1[i].contains(keywords[i]))
            {
                System.out.println("YAY");
            }
            else
                System.out.println("BOO");

        }

Along with everyones solution, If you want to test some java 8 Stream and lambda following is the way you can do it easily 与每个人的解决方案一起,如果您想测试一些Java 8 Streamlambda,则可以轻松实现

 String[] keywords = {"dog", "cat", "monkey", "apple"};

 Arrays.stream(inputArray_1).forEach(
            input -> {
                if (Arrays.stream(keywords).anyMatch(input::equals)) {
                    System.out.println("YAY");
                } else {
                    System.out.println("BOO");
                }
            });
  • First you are taking Stream of your inputArray_1 and iterating it using forEach 首先,您inputArray_1 Stream并使用forEach对其进行迭代
  • Then each of your input is matched against the keywords array. 然后,将您的每个输入与keywords数组进行匹配。 anyMatch here gives you the boolean value true if it exists and false if it not. 这里的anyMatch会为您提供布尔值true如果存在),否则为false You are the simply using that logic to print your outputs 您就是使用该逻辑打印输出的简单工具

If you feel interest about this cool Java 8 stuff. 如果您对这个炫酷的Java 8东西感兴趣。 Dig this Java 8 Stream Tutorial for better understanding how the Stream API and Lambda works. 挖掘此Java 8 Stream教程,以更好地了解Stream APILambda的工作方式。 And also this specific tutorial to Check if Array contains a certain value 还有这个检查数组是否包含特定值的特定教程

Assuming that inputArray_1 is a String array that results from splitting on white spaces, you would need some type of inner loop that checks the current String from the input array, against all Strings from the keywords array. 假设inputArray_1是一个字符串数组,该字符串数组是通过在空格上拆分而得到的,则需要某种内部循环类型,以对照关键字数组中的所有Strings来检查输入数组中的当前String。 So something like: 所以像这样:

        String [] inputArray_1 = new String [] {"word3","word2","word3"};
    String [] keywords = new String [] {"word1","word3"};

    outerloop:
    for(int i = 0; i < inputArray_1.length; i++) {

        for (int j = 0; j < keywords.length; j++) {
            if(inputArray_1[i].equals(keywords[j])){
                System.out.println("YAY");
                continue outerloop;
            }               
        }

        System.out.println("BOO");

    }

在第一个循环内的关键字中使用另一个循环来检查关键字列表中的每个元素。

If the inputArray_1 contains sentences(strings with whitespace and > 1 words), you would want to iterate through each keyword to check if the current sentence, or inputArray_1[i] , .contains() that keyword. 如果inputArray_1包含句子(带有空格和> 1个单词的字符串),则需要遍历每个关键字以检查当前句子或inputArray_1[i] .contains()该关键字。

But if your inputArray_1 just contains values of single words/no sentences, then you could just have one loop like so. 但是,如果您的inputArray_1仅包含单个单词的值/没有句子的值,那么您可能只有这样一个循环。

//This will only work if inputArray_1 contains no sentences, just single words
for(int i = 0; i < inputArray_1 .length; i++)
    {
        if(keywords.contains(inputArray_1[i]))
        {
            System.out.println("YAY");
        }

        else
        {
            System.out.println("BOO");
        }

    }

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

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