简体   繁体   中英

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. 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

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. 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. 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 .

You should only do nextLine once:

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. 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. 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. 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. 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. Similarly, if you enter on different line as well. But, with nextLine, each line as a whole will be added as a new product. 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);
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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