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?