Home > Back-end >  Program appears to skip first iteration of while loop
Program appears to skip first iteration of while loop

Time:10-31

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)

CodePudding user response:

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.

CodePudding user response:

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.

CodePudding user response:

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();

CodePudding user response:

I think your structure of code is not perfect. We don't want to put count variable because using for loop you can reduce your code. We initialize product = 1 because 0 * 5 = 0.

Here down is modified code:

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

CodePudding user response:

you must change scan.nextLine(); to scan.next();

  •  Tags:  
  • java
  • Related