【问题标题】:How to ignore comma and print out elements using Arraylist<String>?如何使用 Arraylist<String> 忽略逗号并打印出元素?
【发布时间】:2021-05-22 10:33:47
【问题描述】:

我想检查输入是否包含"Cheese Sandwich", "Corn Sandwich""Mix Veg Sandwich"。条件是:

  1. 如果输入的输入与Arraylist&lt;String&gt; 中存储的元素匹配,则打印出元素。
  2. 如果输入与Arraylist 中存储的元素不匹配,则打印Incorrect Input
  3. 如果输入可以包含以逗号分隔的长字符串,则必须忽略逗号并打印出逗号之间的字符串。

在我的程序中,我创建了一个列表并将这三个字符串存储在其中,然后循环它们并检查输入是否与存储的元素匹配。问题是当输入以字符串的形式给出,然后用逗号添加这些元素时,它没有给我任何或不正确的输入。如何忽略逗号并将要打印的字符串拆分如下。

示例:

String Input = "Cheese Sandwich, Corn Sandwich, Mix Veg Sandwich"; 

预期输出

cheese sandwich
corn sandwich 
mix veg sandwich

我的计划

static void storeElements(String input) {

    List<String> sandwiches = new ArrayList<>();
    sandwiches.add("corn sandwich");
    sandwiches.add("mix veg sandwich");
    sandwiches.add("cheese sandwich");
    sandwiches.add("mix veg sandwich");
    sandwiches.add("cheese sandwich");
   

    boolean a = sandwiches.contains(input);
    boolean b = input.contains(",");

    List<String> list = new ArrayList(Arrays.asList(input.split(",")));

我不知道怎么做这个ArrayList检查逗号 在字符串输入中,然后验证输入是否与存储的 ArrayList 三明治中的元素之一匹配。

    for (int i = 0; i < input.length(); i++) {
        if (b) {
                for (String c : sandwiches) {
                    if (a) {
                        System.out.println(c);
                    }
                    else {
                        System.out.println("Incorrect Input");
                    }
                }
        }
    }
}

【问题讨论】:

  • 你是什么意思:“将字符串打印到数组列表中”?如果需要检查列表是否包含 c,为什么还要检查 if(a)?
  • 我还有一个问题:if input exist in element..是什么意思?
  • @GiorgiTsiklauri 我想他的意思是:如果元素在列表中,则打印元素。
  • @Stultuske 然后他/她必须澄清,这是否可以是一个空字符串元素。
  • 另外,如果您正在处理“输入”,那么这个 ArrayList 在这种情况下必须做什么?请edit您的问题,使其更清晰并定义您要解决的确切问题。

标签: java validation arraylist input


【解决方案1】:

这是您的初始代码,带有一些 cmets

   static void storeElements(String input) {
            List<String> sandwiches = new ArrayList<>();
            sandwiches.add("corn sandwich");
            sandwiches.add("mix veg sandwich");
            sandwiches.add("cheese sandwich");
            sandwiches.add("mix veg sandwich");
            sandwiches.add("cheese sandwich");               
    
            boolean a = sandwiches.contains(input); // boolean a is set here, and it is never changed. So, if you enter "cheese sandwich, mix veg sandwich" a will never become true.
            boolean b = input.contains(",");
    
            List<String> list = new ArrayList(Arrays.asList(input.split(",")));
    
            for (int i = 0; i < input.length(); i++) {
                if (b) {
                        for (String c : sandwiches) { // You shouldn't be iterating over sandwiches, but over list
                            if (a) { // a is not dependent on your c. this test should only be done if b is false. here, you need to check if an element of list is in sandwiches
                                System.out.println(c);
                            }
                            else {
                                System.out.println("Incorrect Input");
                            }
                        }
                }
            }
        }

修改后的代码

static void storeElements(String input) {
    List<String> sandwiches = new ArrayList<>();
    sandwiches.add("corn sandwich");
    sandwiches.add("mix veg sandwich");
    sandwiches.add("cheese sandwich");
    sandwiches.add("mix veg sandwich");
    sandwiches.add("cheese sandwich");

    boolean a = sandwiches.contains(input);
    boolean b = input.contains(",");

    List<String> list = new ArrayList(Arrays.asList(input.split(", ")));


        if (b) {
            for (String c : list) {
                System.out.println(sandwiches.contains(c) ? c : "Incorrect Input");
            }
        } else {
            System.out.println(a ? input : "Incorrect Input"); // here it makes sense to check if a is true
        }
}

可以简化为:

static void storeElements(String input) {
    List<String> sandwiches = new ArrayList<>();
    sandwiches.add("corn sandwich");
    sandwiches.add("mix veg sandwich");
    sandwiches.add("cheese sandwich");
    sandwiches.add("mix veg sandwich");
    sandwiches.add("cheese sandwich");

    List<String> list = new ArrayList(Arrays.asList(input.split(", ")));
    for (String c : list) {
      System.out.println(sandwiches.contains(c) ? c : "Incorrect Input");
    }
}

在我原来的答案中,我有 split(","),这是我从原件复制而来的,但我忽略了这一点:

如果您对“奶酪三明治,玉米三明治”执行 split(","),您将得到“奶酪三明治”和“玉米三明治”。第一个会被找到,第二个不会,因为“corn”与“corn”不同,所以空格会有所不同。

编辑:仅在不匹配时打印不正确的输入。

static void storeElements(String input) {
    List<String> sandwiches = new ArrayList<>();
    sandwiches.add("corn sandwich");
    sandwiches.add("mix veg sandwich");
    sandwiches.add("cheese sandwich");
    sandwiches.add("mix veg sandwich");
    sandwiches.add("cheese sandwich"); // this line is not relevant, the value is already in sandwiches

    List<String> list = new ArrayList(Arrays.asList(input.split(", ")));
    List<String> result = new ArrayList<>();
    
    for (String c : list) {
      if ( sandwiches.contains(c)) {
          result.add(c); // only add the String if it is found in the original list
      } else {
              result.clear();
              break;
      }
    }

    if ( result.isEmpty()) {
        // this will only happen if no elements have been added to the result list
        System.out.println("Incorrect input");
    } else {
        for ( String found : result ) {
            System.out.println(found);
        }
    }
}

【讨论】:

  • 它在输入等于元素的 on 时起作用,但当字符串为“Cheese Sandwich, Corn Sandwich, Mix Veg Sandwich”时,输出与“Incorrect Input”混合在一起
  • @Waelalmerri 那是因为“奶酪三明治”和“奶酪三明治”不一样。 contains方法使用的equals区分大小写
  • 我也试过小写,结果一样
  • @Waelalmerri 我检查了(代码写得很快)并发现了问题,我会用更好的解决方案编辑我的答案
  • 感谢它有效,不过是一个问题。它会遍历每个单词并打印出来,即使是“输入错误”。仅当给出与列表不匹配的任何错误输入时,该函数才必须打印输出“错误输入”。
【解决方案2】:

您可以通过意识到您只需要考虑一种输入来大大简化事情:以逗号分隔的项目列表。 "Cheese Sandwich, Corn Sandwich, Mix Veg Sandwich" 显然是一个逗号分隔的列表,包含三个项目。 "Cheese Sandwich" 不太明显,它是 _also,一个逗号分隔的列表,只有一个项目。

因此,只需将 所有 输入作为逗号分隔的列表处理,一切都会变得不那么复杂:

static void storeElements(String input) {

        List<String> sandwiches = new ArrayList<>();
        sandwiches.add("corn sandwich");
        sandwiches.add("mix veg sandwich");
        sandwiches.add("cheese sandwich");
        sandwiches.add("mix veg sandwich");
        sandwiches.add("cheese sandwich");
       
        List<String> list = new ArrayList(Arrays.asList(input.split("\\s*,\\s*")));

        for (String item : list) {
            item = item.toLowerCase();
            if (sandwiches.contains(item) {
                System.out.println("Yes, we have " + item);
            } else {
                System.out.println("Incorrect input");
            }
        }
}

【讨论】:

  • 在您的代码中,它只检查第一个单词,其余两个返回不正确的输入
  • 我稍微修改了代码,以处理逗号前后的空格,以及大写与小写的问题。
  • 感谢它有效,不过是一个问题。它会遍历每个单词并打印出来,即使是“输入错误”。仅当给出与列表不匹配的任何错误输入时,该函数才必须打印输出“错误输入”。
猜你喜欢
  • 1970-01-01
  • 2013-10-14
  • 1970-01-01
  • 1970-01-01
  • 2021-10-25
  • 1970-01-01
  • 2019-05-17
  • 2014-06-29
  • 2023-03-08
相关资源
最近更新 更多