检查String x是否等于String []中的任何字符串

时间:2013-08-04 15:20:38

标签: java

如果用户输入的字符串等于字符串数组中的任何字符串,我试图设置一个布尔值。

我有即兴创作并提出了这个

String[] cancelWords = {"cancel", "nevermind", "scratch that"};
boolean valueEqualsCancel = true;
for(String cancelWord : cancelWords) {
  if(!valueEqualsCancel) break;
  valueEqualsCancel =  valueEqualsCancel && value.equals(cancelWord);
}

valueEqualsCancel永远不会成真。

任何提示?

12 个答案:

答案 0 :(得分:17)

您可以执行以下操作:

Arrays.asList(cancelWords).contains(value)

(见Arrays.asList()

如果您要执行此类多个查询,请将所有字词放在Set中并查询。

答案 1 :(得分:4)

valueEqualsCancel 永远不会为真,因为当您找到 cancelWord 时,您不会退出循环。

为了达到break语句,你需要将valueEqualsCancel设为false。

如果您在第一个循环后搜索“cancel”,则变量valueEqualsCancel为:

valueEqualsCancel =  valueEqualsCancel && value.equals(cancelWord) = TRUE && TRUE = TRUE;

所以在第二个循环中你不会破坏。然后再次评估表达式

valueEqualsCancel =  valueEqualsCancel && value.equals(cancelWord) = TRUE && FALSE = FALSE;      

因此,在第三个循环中,您将退出并且valueEqualsCancel为false。

您可以通过以下方式更正代码:

String[] cancelWords = {"cancel", "nevermind", "scratch that"};
boolean found = false;
for(String cancelWord : cancelWords) {
  found = value.equals(cancelWord);
  if (found) break;
}

答案 2 :(得分:4)

这是一个使用Java 8的2-liner,易于阅读:

String[] cancelWords = {"cancel", "nevermind", "scratch that"};
boolean valueEqualsCancel = Arrays.stream(cancelWords).anyMatch(value::equals);

答案 3 :(得分:2)

Array转换为List,然后使用contains方法进行检查。

Arrays.asList(cancelWords).contains(value)

答案 4 :(得分:0)

 foreach(string thisWord in cancelWords)
    if thisWord.equals(value)
       return true;

 return false;    // Fall-through

答案 5 :(得分:0)

您的初始值已关闭,可以改进流程:

String[] cancelWords = {"cancel", "nevermind", "scratch that"};
boolean valueEqualsCancel = false; // fix
for(String cancelWord : cancelWords) {
  if(value.equals(cancelWord)) {
    valueEqualsCancel =  true;
    break;
  }
}

答案 6 :(得分:0)

使用此方法:

public boolean containsWord(String word, String[] words) {
   for(String cancelWord : words) {
      if(word.equals(cancelWord)) {
        return true;
      }
   }
   return false;

}

我希望这会有所帮助。

答案 7 :(得分:0)

我想这应该会有所帮助: -

import java.util.Scanner;


public class check4 {
    public static void main(String args[])
    {
       Scanner scan = new Scanner(System.in);
       String value = scan.nextLine().toString();
       String[] cancelWords = {"cancel", "nevermind", "scratch that"};
       boolean valueEqualsCancel = false;
       for(int i=0;i<cancelWords.length;i++) {
          if(value.equals(cancelWords[i])) {
             valueEqualsCancel = true;
             break;
          }
       }
       System.out.println(valueEqualsCancel);
    }
}

输入:

camel

输出:

true

答案 8 :(得分:0)

尝试

 String[] cancelWords = {"cancel", "nevermind", "scratch that"};     
  boolean valueEqualsCancel = true;     
  for(String cancelWord : cancelWords) {      
  if(!valueEqualsCancel) break;     
   valueEqualsCancel =  !(valueEqualsCancel && value.equals(cancelWord));     
 }

答案 9 :(得分:0)

使用此方法

public static boolean  isContainsWords(String[] words,String Targent){

               boolean flag;
               for(String buf : words){
                     if(buf.equals(Targent))
                          return flag = true;
               }
               return flag = false;

}

看起来很完美

答案 10 :(得分:0)

最适合您情况的解决方案取决于取消字的数量以及比较不区分大小写的必要性。我为两种可能的实现方式添加了代码:

// I would use a HashSet which needs constant time to compare, even with long lists of cancel words
// Note, that the use of toLowerCase() makes the comparison case insensitive.
@Test
public final void test2() {
    String value = "nevermind";
    HashSet<String> cancelWords = new HashSet<String>();
    cancelWords.addAll(Arrays.asList(new String[] {"cancel", "nevermind", "scratch that"}));

    boolean valueEqualsCancel = cancelWords.contains(value.toLowerCase());
    System.out.println("test2: " + valueEqualsCancel);
}

// You might like to know, which cancel word it was
@Test
public final void test3() {
    String value = "nevermind";
    String[] cancelWords = {"cancel", "nevermind", "scratch that"};
    Arrays.sort(cancelWords); // Prepare for binary search

    // The returned value indicates either which cancel word was used or, that nothing was found with result < 0.
    System.out.println("test3: nevermind " + Arrays.binarySearch(cancelWords, "nevermind"));
    System.out.println("test3: cancel " + Arrays.binarySearch(cancelWords, "cancel"));
    System.out.println("test3: something totally different " + Arrays.binarySearch(cancelWords, "something totally different"));
}

答案 11 :(得分:0)

如果您有Apache Commons Lang,则可以使用StringUtils.equalsAny()

String[] cancelWords = {"cancel", "nevermind", "scratch that"};
boolean valueEqualsCancel = StringUtils.equalsAny(value, cancelWords);