0

The homework problem is to write a while loop that prompts the user to enter as many integers as they want and to type "q" when done, then print the sum of those integers and the number of entries.

This is what I have so far... It doesn't work if the user inputs more than one integer and I don't know why.

package chapter06lab;

import java.util.Scanner;

public class ProgramF5
{

    public static void main(String[] args)
    {
        Scanner one = new Scanner(System.in);
        System.out.println("Please enter as many valid integers as you wish ('q' to finish): ");
        int a = one.nextInt();
        int count = 0;
        int sum = 0;
        while(one.hasNextInt())
        {
            count++;
            sum =+ a;
        }
        
        System.out.println("The number of entries was: " + count);
        System.out.println("The sum of all numbers entered is: " + sum);
    }

}
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • Perhaps you should call `one.nextInt()` inside that loop? – Some programmer dude Dec 03 '20 at 03:22
  • you are exactly right... after I applied that correction, I met one last problem. The sum that was being printed was only the value of the last inputted integer. So for example, if 35, 45, and 65 were inputted, only 65 was printed as the sum. – JT Marchal Dec 03 '20 at 03:28

2 Answers2

0

Try this:

public static void main(String[] args)
    {
        Scanner one = new Scanner(System.in);
        System.out.println("Please enter as many valid integers as you wish ('q' to finish): ");
        int count = 0;
        int sum = 0;
        while(true)
        {
            String input = one.next();
            if(input.equals("q")){
                break;
            }else{
                try{
                    int x = Integer.parseInt(input);
                    sum+=x;
                    count++;
                }catch (Exception ex){
                    System.out.println("You entered an invalid integer,try again.");
                }
            }
        }

        System.out.println("The number of entries was: " + count);
        System.out.println("The sum of all numbers entered is: " + sum);
    }
shenzhigang
  • 103
  • 8
  • Code-only answers are discouraged as they tend to promote cargo cult programming. What changes did you make? Why did you make them? Where did you make them? Do you really need to show the whole `main` function, instead of only the modified parts? And please take some time to read about [how to write good answers](https://stackoverflow.com/help/how-to-answer). – Some programmer dude Dec 03 '20 at 03:34
  • I has changed the answer, you can try it. – shenzhigang Dec 03 '20 at 03:43
  • it's not about where you are right now, it's about how you learn the problem @JTMarchal – ariefbayu Dec 03 '20 at 03:59
  • First, 'int a = one.nextInt();' is not in the loop, so it will just execute one time. Second, one.nextInt() will throw exceptions if the input string is not valid integer. – shenzhigang Dec 03 '20 at 04:15
0

To answer your question in the comment:

you are exactly right... after I applied that correction, I met one last problem. The sum that was being printed was only the value of the last inputted integer. So for example, if 35, 45, and 65 were inputted, only 65 was printed as the sum.

You should move your a initialization to inside of while() loop. Why?

Because you need to always get the latest int in the loop, that is why a need to be set inside loop.

Therefor, your while should looks like this:

while(one.hasNextInt())
{
    int a = one.nextInt();
    count++;
    sum =+ a;
}
ariefbayu
  • 21,849
  • 12
  • 71
  • 92