Home > Enterprise >  While Loops in Java Problems
While Loops in Java Problems

Time:10-26

Now we are learning about while loops - here is the challenge. "the program should repeatedly prompts the user to enter a number. Keep a running total of the numbers the user enters and also keep a count of the number of entries the user makes. The program should stop whenever the user enters something that is not an integer. When the user has finished, print the number of entries the user has typed and their sum."

"A sample interaction with this program might look like this: Program: Enter an integer to continue or a non-integer value to finish. Then press return.

User: 2

Program: Enter an integer to continue or a non-integer value to finish. Then press return.

User: 6

Program: Enter an integer to continue or a non-integer value to finish. Then press return.

User: 89

Program: Enter an integer to continue or a non-integer value to finish. Then press return.

User: q

Program: You entered 3 integers. The sum of your entries is 97."

Here's what I have so far.

import java.util.Scanner;
public class InputSequence
{
    public static void main (String[] args)
    {
        Scanner scan = new Scanner(System.in);
        System.out.println ("Enter an integer to continue or a non-integer value to finish. Then press return.");
        int sum = 0;
        int total = 0;
        while (scan.hasNextInt())
        {
            scan.next();
            System.out.println ("Enter an integer to continue or a non-integer value to finish. Then press return.");
            int input = scan.nextInt();
            sum = input   input;
            total = total   1;
        }
        System.out.println ("You entered "   total   " integers. The sum of your entries is "   sum);
    }
}

I'm just a bit confused because my interaction seems to skip the system.out.println every 2nd time in the while loop and then doesn't count the first input into the total. I believe the sum also doesn't always calculate correctly. Also, if I input a lot of integers then a non-integer to end the program I will receive an error rather than the end message. However, let's say I put in two integers then a string, I will get the end message.

CodePudding user response:

Your problem is in the usage in scan.next() that can be removed and should show the System.out.println correctly.

.next() method is a generic method of nextInt() which returns a String instead.

Another thing you can do is to switch from a while loop to do while to prevent you from repeating this line twice:

System.out.println ("Enter an integer to continue or a non-integer value to finish. Then press return.");

an example would be:

Scanner scan = new Scanner(System.in);
        int sum = 0;
        int total = 0;
        do {
            System.out.println ("Enter an integer to continue or a non-integer value to finish. Then press return.");
            int input = scan.nextInt();
            sum = input   input;
            total = total   1;
        } while (scan.hasNextInt());
        System.out.println ("You entered "   total   " integers. The sum of your entries is "   sum);
    }
  • Related