0

I am completely new to programming and taking a Java 101 in my Uni and have been fighting with this problem for an hour now, searching web and not understanding what is wrong.

So the exercise is to make a program that prompts numbers, reads them and sums them until the user inputs a 0 and the program terminates. My problem is that my program ignores the first number I input, I always have to input it twice. It seems the loop is the problem, but how would I know? Here is the my code so far:

import java.util.Scanner;

public class SumOfMultipleNumbers {

    public static void main(String[] args) {
        Scanner reader = new Scanner(System.in);
        int sum = 0;               

        while (true) {
            int read = Integer.parseInt(reader.nextLine());
            if (read == 0) {
                break;
            }

            read = Integer.parseInt(reader.nextLine());
            sum += read;

            System.out.println("Sum now: " + sum);
        }

        System.out.println("Sum in the end: " + sum);
    }
}

So how do I correct my code so that it adds to the sum every time I enter a number and not every second time?

4 Answers 4

1

That's because you read a new line after checking for zero input.

while (true) {
    int read = Integer.parseInt(reader.nextLine());
    if (read == 0) {
        break;
    }

    // this is not needed: read = Integer.parseInt(reader.nextLine());
    sum += read;
Sign up to request clarification or add additional context in comments.

Comments

1

you're calling

read = Integer.parseInt(reader.nextLine());

twice per loop

once when you declare read

int read = Integer.parseInt(reader.nextLine());

and then again, after you check for zero

read = Integer.parseInt(reader.nextLine());

You only need to do it once


What you probably want to use is a do while loop

do
{
    // read variable in
    // print for sum
}while(variableReadIn != 0);

Comments

0

You're calling reader.nextLine() at the beginning of the loop, for the purpose of determining whether you should break the loop, then you do it again in order to get the actual number to sum. That's the reason your code is ignoring the inputted value the first time.

What you want to do is call reader.nextLine() only once, and check that value for your exit condition. I might do something like this:

while (true) {
    String inputText = reader.nextLine();

    if(inputText.equals("0")){
        break;
    }

    int read = Integer.parseInt(inputText);
    sum += read;

    System.out.println("Sum now: " + sum);

}

Comments

0

Each time you call reader.nextLine() a new line is read from the Scanner, you call this function before checking for zero and before adding it to the sum.

It is better practice to not just use a while(true) loop, you should instead use a loop with a condition like while(read != 0)

Finally, you should close the Scanner to prevent corruption. In this program it can't do very much harm, but that doesn't mean you can forget it.

import java.util.Scanner;

public class SumOfMultipleNumbers {

    public static void main(String[] args) {
        Scanner reader = new Scanner(System.in);

        int sum = 0;

        int read;

        do {
            sum += read;

            System.out.println("Sum now: " + sum);

            read = Integer.parseInt(reader.nextLine());
        } while (read != 0)

        System.out.println("Sum in the end: " + sum);

        reader.close();
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.