【问题标题】:Is it possible to compare an array string to a normal string?是否可以将数组字符串与普通字符串进行比较?
【发布时间】:2016-02-11 19:09:12
【问题描述】:

我正在尝试将(字符串)数组与输入中的常规字符串进行比较。这可能吗?我有我的测试代码,它大多只是说

incomparable types java.lang.string and java.lang.string[]

这是我的测试代码:

 String[] yes = new String[5];
yes[0] = "yes";
yes[1] = "yeah";
yes[2] = "sure";
yes[3] = "yupp";
yes[4] = "okay";
System.out.println("Input");
String input = scan.next();
if ((input).equalsIgnoreCase(yes)) {
  System.out.println("compared!");
}

【问题讨论】:

    标签: java arrays compare user-input


    【解决方案1】:

    最有效的解决方案是创建一组字符串,然后调用 set.contains 来查看输入字符串是否存在于集合中。示例代码:

     HashSet<String> stringSet = new HashSet<String>();
        //All string are already lower case so no need of toLowerCase() here
        stringSet.add("yes");
        stringSet.add("yeah");
        stringSet.add("sure");
        stringSet.add("yupp");
        stringSet.add("okay");
        System.out.println("Input");
        String input = scan.next();
        if (stringSet.contains(input.toLowerCase())) {
          System.out.println("compared!");
        }
    

    【讨论】:

    • 您需要将输入转换为小写才能使其正常工作。它还要求集合中的字符串是小写的(这里就是这种情况,但应该提到......)。
    • 建议:Set&lt;String&gt; stringSet = new HashSet&lt;String&gt;();(接口代码)然后:Collections.addAll(stringSet, "yes", "yeah", "sure", "yupp", "okay");
    • @fabian 字符串比较区分大小写。我已经为你添加了一个测试代码。你可以检查这个ideone.com/iJRNTN
    • @AbhishekGarg:我知道字符串比较是区分大小写的,但是由于 OP 想要使用 equalsIgnoreCase,您需要一个解决方法来使用 Set。在这种情况下,只需将每个字符串转换为小写即可,因为s1.equalsIgnoreCase(s2) iff s1.toLowerCase().equals(s2.toLowerCase())(假设不涉及null 对象)
    • 哦,好吧,我错过了 equalsIgnoreCase 部分。感谢编辑
    【解决方案2】:

    您需要指定要检查的数组中的哪个索引。

    数组中的所有五个条目都是不同的,Java 不知道要检查哪一个。

    您可能希望使用for 循环遍历所有可能性,并将每个条目与输入进行比较。

    【讨论】:

    • 是否有一种简单的方法可以将它们全部一起检查,或者它是否类似于 if((input).equalsIgnoreCase(yes[0,1,2,3,4])) {
    • 这是一个三班轮。 for (String possibility : yes)if (input.equalsIgnoreCase(possibility))System.out.println("Compared!");
    【解决方案3】:

    我会使用某种形式的迭代来一次遍历数组一个元素:

    for (String compare : yes){
        if ((input).equalsIgnoreCase(compare)) {
        System.out.println("compared!");
    }}
    

    或者在你读入输入之后类似的东西。

    【讨论】:

      【解决方案4】:

      假设yes 中的值都是小写的

      String[] yes = new String[]{"yes", "yeah", "sure", "yupp", "okay"};
      System.out.println("Input");
      String input = scan.next();
      if (Arrays.asList(yes).contains(input.toLowerCase())) {
        System.out.println("compared!");
      }
      

      或使用 lambda,忽略大小写,如 cmets 中的 fabian 所建议的

      String[] yes = new String[]{"yes", "yeah", "sure", "yupp", "okay"};
      System.out.println("Input");
      String input = scan.next();
      if (Arrays.stream(yes).anyMatch(input::equalsIgnoreCase)) {
        System.out.println("compared!");
      }
      

      【讨论】:

      • Arrays.stream(yes).anyMatch(input::equalsIgnoreCase) 不需要 yes 数组全部为小写。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-22
      • 1970-01-01
      • 2016-10-17
      • 1970-01-01
      • 2021-06-10
      • 1970-01-01
      相关资源
      最近更新 更多