简体   繁体   English

为什么我必须写两次才能在 Arraylist 中添加一个输入?

[英]Why do I have to write twice to add an input in the Arraylist?

public static void main(String[] args) {
    List<String> arrayList = new ArrayList<>();
    Scanner input = new Scanner(System.in);
    do {
        System.out.println("Enter a product");
        String product = input.nextLine();
        arrayList.add(product);
    }
    while (!input.nextLine().equalsIgnoreCase("q"));

    System.out.println("You wrote the following products \n");
    for (String naam : arrayList) {
        System.out.println(naam);
    }
}

I'm trying to get some input from the user and store them into arraylist.我试图从用户那里获得一些输入并将它们存储到 arraylist 中。 The problem is I have to write the item twice to add an item into the list.问题是我必须将项目写入两次才能将项目添加到列表中。 I can't figure out why!我不知道为什么!

Instead of do-while loop use only while而不是do-while循环仅使用while

while (true){
    System.out.println("Enter a product");
    String product = input.nextLine();
    if (!product.equalsIgnoreCase("q"))
        arrayList.add(product);
    else
        break;    
}

Every time you write readLine() , a line is read.每次编写readLine()时,都会读取一行。 In this loop,在这个循环中,

do {
  System.out.println("Enter a product");
  String product = input.nextLine();
  arrayList.add(product);
}
while (!input.nextLine().equalsIgnoreCase("q"));

There are two occurrences of readLine() , so two lines are read every iteration. readLine()出现了两次,因此每次迭代都会读取两行。 The first line is always added to the list and not checked against q , and the second is never added to the list, and always checked against q .第一行始终添加到列表中,并且不检查q ,第二行永远不会添加到列表中,并且始终检查q

You should only do nextLine once:你应该只做一次nextLine

while (true) {
    System.out.println("Enter a product");
    String product = input.nextLine(); // only this one time!
    if (!product.equalsIgnoreCase("q")) {
        arrayList.add(product);
    } else {
        break;
    }
}

It happenes coz input.nextLine() makes java read the input.发生它是因为input.nextLine()使 java 读取输入。 You should read the line and only then do the stuff:您应该阅读该行,然后才执行以下操作:

Scanner input = new Scanner(System.in);
String product = input.nextLine();
System.out.println("Enter a product");
while (!product.equalsIgnoreCase("q")) {    
    arrayList.add(product);
    System.out.println("Enter a product");
    product = input.nextLine();
}

You can read the String values using input.next() once and have a while loop in place and read further values into your list only if the value is not equal to q.您可以使用 input.next() 读取字符串值一次,并设置一个 while 循环,并且仅当值不等于 q 时才将更多值读取到您的列表中。 If you have read it twice as in your case, one value is added to the list in your do part, and the value you read again in your while part is only compared to q and so to exit your code, you will be missing one value and adding another and have to give two q values one after another to exit it.如果您已经阅读了两次,则在您的 do 部分中将一个值添加到列表中,并且您在 while 部分中再次读取的值仅与 q 进行比较,因此要退出您的代码,您将丢失一个值并添加另一个,并且必须一个接一个地给出两个 q 值才能退出它。 Also, since most of the other users have given there answers with nextLine instead of next you may want to check what next does and what nextLine does.此外,由于大多数其他用户已经给出了 nextLine 而不是 next 的答案,您可能需要检查 next 做什么以及 nextLine 做什么。 In brief, if you enter names of products separated by a delimiter (default space), then with next, each value separated by the space is considered a product.简而言之,如果您输入由分隔符(默认空格)分隔的产品名称,那么在 next 中,每个由空格分隔的值都被视为一个产品。 Similarly, if you enter on different line as well.同样,如果您也输入不同的行。 But, with nextLine, each line as a whole will be added as a new product.但是,使用 nextLine,每条线作为一个整体将被添加为一个新产品。 It depends on how you may want to achieve this as per your requirement.这取决于您可能希望如何根据您的要求实现这一目标。

    public static void main(String[] args) {
        List<String> arrayList = new ArrayList<>();
        Scanner input = new Scanner(System.in);

        String product = input.next();

        while(!product.equalsIgnoreCase("q")) {
            arrayList.add(product);
            product = input.next();
        }

        System.out.println("You wrote the following products \n");
        for (String naam : arrayList) {
            System.out.println(naam);
        }
    }

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

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