2

So in my code I am trying to add numbers together in a loop, what I mean is that once you enter a number it adds it to the previous and gives you the new sum. I have this working fine, but when I enter a 0, which ends the program, giving you a message saying whatever the last sum was, it restarts the program, and I can't seem to make it end. Here is the website which I am using to get my work from. I am on exercise 26.

Here is my code:

package July28Restart;

import java.util.Scanner;

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

        int sum = 0;
        while (true){

            System.out.println("Enter numbers one by one, and continue entering them. The system will continue adding them together with the last sum. When you enter 0, the last sum will be the last.");
            int read = Integer.parseInt(reader.nextLine());

            if (read == 0){
                break;
            } else if (read != 0){
                while (true){
                    sum = sum + read;
                    System.out.println("Sum now: " +sum);
                    read = Integer.parseInt(reader.nextLine());
                    if (read == 0) {
                        System.out.println("The sum in the end was: "+sum);
                        break;              
                    }   
                }   
            }
        }
    }
}

Any help would be much appreciated.

Ryan
  • 1,972
  • 2
  • 23
  • 36
FreezieX17
  • 45
  • 6

3 Answers3

2

Because if you enter 0 after entering another number you break from the second loop to to first, and only if you enter 0 twice (directly after another) you will exit

Corrected code:

package July28Restart;
import java.util.Scanner;

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

        int sum = 0;
        while (true) {

            System.out.println(
                    "Enter numbers one by one, and continue entering them. The system will continue adding them together with the last sum. When you enter 0, the last sum will be the last.");
            int read = Integer.parseInt(reader.nextLine());

            if (read == 0) {
                exitProgram(sum);
            } else if (read != 0) {
                while (true) {
                    sum = sum + read;
                    System.out.println("Sum now: " + sum);
                    read = Integer.parseInt(reader.nextLine());
                    if (read == 0) {
                        exitProgram(sum);
                    }

                }

            }

        }

    }

    private static void exitProgram(int sum) {
        System.out.println("The sum in the end was: " + sum);
        System.exit(0);

    }

}
Manuel Manhart
  • 4,819
  • 3
  • 24
  • 28
  • 1
    Don't use `System.exit` - it puts a serious limitation on re-usability and extending of the code. Also, don't use `boolean` value to control your loops like that, this is what labels are for: you just add `outer:` before the outer loop and change your `return;` to `return outer;`. Job done with just one extra line of code. – Jaroslaw Pawlak Aug 31 '17 at 16:11
  • I didn't want to optimize the code so the TO would recognize his code & see the code changes. But for real projects your comment is absolutely valid and helpful. – Manuel Manhart Aug 31 '17 at 16:15
2

You can just use labels to break outer loop from within inner loop:

outer:
while (true){
    // ...
    while (true){
        // ...
        if (read == 0) {
            System.out.println("The sum in the end was: "+sum);
            break outer;              
        }   
    }   
    // ...
}
Jaroslaw Pawlak
  • 5,538
  • 7
  • 30
  • 57
2

Sorry, just saw your question. As Jaroslaw Pawlak explained, you can used outer here or you can refactor and use one while only. Something looks like:

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

    }

    public static void calSum(Scanner reader) {
        System.out.println("Enter numbers one by one, and continue entering them. The system will continue adding them together with the last sum. When you enter 0, the last sum will be the last.");
        int sum = 0;
        while (true){
             int read = Integer.parseInt(reader.nextLine());
             if(read != 0) {
                 sum = sum + read;
                 System.out.println("Sum now: " +sum);
             } else {
                 break;
             }
        }
        System.out.println("The sum in the end was: " + sum);
    }
}
Kenny Tai Huynh
  • 1,464
  • 2
  • 11
  • 23