0

What is causing my program to skip the first iteration of my while loop here?:

    import java.util.Scanner;

public class myfun {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int totalNumbers;
        int product = 0;
        int count = 0;
        int sum = 0;
        String choice = "choice";
        System.out.print("You can add up to ten numbers.\n");
        while(! choice.equalsIgnoreCase("stop")){
            System.out.print("Would you like to add or multiply? (Enter stop to be done)");
            choice = scan.nextLine();
            if (choice.equalsIgnoreCase("multiply")) {
                product = 0;
                System.out.print("How many numbers would you like to multiply?");
                totalNumbers = scan.nextInt();
                count = 0;
                System.out.print("Please enter " + totalNumbers +" numbers.");
                product +=scan.nextInt();
                count++;
                while (count < totalNumbers) {
                    product *= scan.nextInt();
                    count++;
                }
                System.out.println("The product is " + product);
            }
            else if (choice.equalsIgnoreCase("add")) {
                sum = 0;
                System.out.print("How many numbers would you like to add?");
                totalNumbers = scan.nextInt();
                count = 0;
                System.out.print("Please enter " + totalNumbers +" numbers.");
                sum +=scan.nextInt();
                count++;
                    while (count < totalNumbers) {
                        sum += scan.nextInt();
                        count++;
                    }
                System.out.println("The sum is " + sum);
            }
        }
        System.out.print("Come again!");
        scan.close();
    }
}

here's an example output

You can add up to ten numbers. Would you like to add or multiply? (Enter stop to be done) add How many numbers would you like to add? 2 Please enter 2 numbers.2 2 The sum is 4 Would you like to add or multiply? (Enter stop to be done)Would you like to add or multiply? (Enter stop to be done)

  • Does this answer your question? [Scanner is skipping nextLine() after using next() or nextFoo()?](https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo) – sittsering Oct 31 '21 at 08:27

3 Answers3

0

Replace you call to scan.nextLine() with scan.next(). The issue with Scanner.nextLine() is that it captures the newline character added after the last integer (2 in you example).

In your case, you just want one word input(add/multiple/stop), so you can just use Scanner.next() function and it will work fine.

For more info, you can go through this SO thread.

Arpit Shukla
  • 9,612
  • 1
  • 14
  • 40
  • We don't want to change `nextLine()` to `next()` because problem is create when `nextInt()` is before `nextLine()` check it... – Faheem azaz Bhanej Oct 31 '21 at 07:44
  • That's what I said. When u call `nextInt()`, it asks for an input, you input an integer and press enter (which is essentially a new line character). Now this newline character is consumed by the next `nextLine()` and `choice` becomes empty, both conditions evaluate to false, and while loop runs again asking for another input. – Arpit Shukla Oct 31 '21 at 08:38
  • I can not change `scan.nextLine()` to `scan.next()` but it work well see below my answer – Faheem azaz Bhanej Nov 01 '21 at 03:39
0

This set of instructions are behaving as expected. scan.nextLine() reads value till it finds line separator. since you are scanning line before any input, execution controller will not move further till it finds line separator.

0

Root Cause : nextLine() method takes the empty string i.e. "" as input in your iteration.

Solution : You can use the next() method to solve this issue.

Modify the following line

choice = scan.nextLine();

to

choice = scan.next();
Sharad Nanda
  • 406
  • 1
  • 15
  • 27