java 如何将用户输入添加到 ArrayList 直到关键字触发它停止?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28082288/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 12:58:55  来源:igfitidea点击:

How to add user input into an ArrayList until a keyword triggers it to stop?

javaarraylistjava.util.scanner

提问by Michael Wendel

So I'm trying to write a Java program that allows a user to input words at the command line. The program should stop accepting words when the user enters "STOP". Store the words in an ArrayList. The word STOP should not be stored in the list.

所以我正在尝试编写一个 Java 程序,允许用户在命令行输入单词。当用户输入“STOP”时,程序应该停止接受单词。将单词存储在 ArrayList 中。单词 STOP 不应存储在列表中。

Next, print the size of the list, followed by the contents of the list.

接下来,打印列表的大小,然后是列表的内容。

Then, remove the first and last words stored in the list, but only if the list has a length greater than two. Finally, reprint the contents of the list.

然后,删除存储在列表中的第一个和最后一个单词,但前提是列表的长度大于 2。最后,重新打印列表的内容。

import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;
public class t2_lesson1_template {

    public static void main (String str[]) throws IOException
    {
        ArrayList<String> list  = new ArrayList<String>();

        Scanner scan = new Scanner(System.in);  


        do 
        {
            System.out.println("What would you like to add to the list?"); 
            String input = scan.nextLine();
            list.add(input);
        }

        while( scan.nextLine() != "STOP");


       if ( list.size() < 2)
       {
        System.out.println(list);
        System.out.println(list.size());
       }
       else
       {
        list.remove(0);
        list.remove(list.size()-1);
        System.out.println(list);
        System.out.println(list.size());
       }
   }

}

It keeps on prompting the question, but never recognizes when "STOP" is the input. If somebody could please help me figure out what's wrong, it'd help a lot. Thank you!

它不断提示问题,但从不识别何时输入“STOP”。如果有人能帮我弄清楚出了什么问题,那会很有帮助。谢谢!

回答by TheLostMind

Change scan.nextLine() != "STOP"to while(!scan.nextLine().equalsIgnoreCase("stop"));and try.

更改scan.nextLine() != "STOP"while(!scan.nextLine().equalsIgnoreCase("stop"));和尝试。

Reason :

原因 :

The String Literal"STOP"will not be be same as the String value "STOP"entered from the keyboard (in your case). ==compares references. You have to check value of 2 Strings not references.

字符串文字"STOP"不会是相同的字符串值"STOP"从键盘输入(你的情况)。==比较参考。您必须检查 2 个字符串的值而不是引用。

回答by Vitaly

In line:

排队:

scan.nextLine() != "STOP"

you compare references to two objects. If you need to compare objects you should use equals() method of Object.

您比较对两个对象的引用。如果你需要比较对象,你应该使用 Object 的 equals() 方法。

Read about equals() in the documentation.

阅读文档equals() 。

But there is another problem in your code. You read next line twice. In loop and in while(...) statement.

但是您的代码中还有另一个问题。你读下一行两遍。在循环和 while(...) 语句中。

Try this:

试试这个:

System.out.println("What would you like to add to the list?");
String input = scan.nextLine();
while(!input.equals("STOP"))
{
    list.add(input);
    input = scan.nextLine();
}

回答by shalin

Try the below code:-

试试下面的代码:-

import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;

public class t2_lesson1_template {

  public static void main(String str[]) throws IOException {
    ArrayList<String> list = new ArrayList<String>();

    Scanner scan = new Scanner(System.in);

    System.out.println("What would you like to add to the list?");
    String input = scan.nextLine();

    while (!input.equals("STOP")) { // compare string using equals(Object o) method
      list.add(input);

      System.out.println("What would you like to add to the list?");
      input = scan.nextLine();
    }

    System.out.println("Size of list = " + list.size());

    System.out.println("Input list:-\n" + list);

    if (list.size() > 2) {
      list.remove(0);
      list.remove(list.size() - 1);

      System.out.println("List after removing first and last eliment:-\n" + list);
    }
  }

}