0

I'm trying to store the sum of 2 numbers inside a while loop so that once the loop ends multiple sums can be added up and given as a total sum, however I am rather new to Java and am not sure how to go about doing this. I'm trying to use an array but I'm not sure if it is the correct thing to use. Any help would be greatly appreciated.

import java.util.Scanner;
public class StoredWhile{
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int TotalNum[]=new int[10];
        Int Num1, Num2, AddedNum;
        String answer;
        do{
            System.out.println("Please enter a number");
            Num1 = input.nextInt();
            System.out.println("Please enter a second number");
            Num2 = input.nextInt();
            AddedNum = Num1 + Num2;
            System.out.println("The sum of the two entered numbers is " + AddedNum);

            TotalNum[0]=AddedNum;
            TotalNum[1]=;

            System.out.println("Would you like to calculate the sum of two more numbers (y/n)?");
            answer = input.next();
        }
        while (answer.equals("y"));
        System.out.println("The total sum of all the numbers you entered is " + TotalNum);
    }
}

2 Answers 2

3

There is a data container called ArrayList<>. It is dynamic and you can add as many sums as you need.

Your example could be implemented like this:

public class StoredWhile{
        public static void main(String[] args) {
            Scanner input = new Scanner(System.in);
            ArrayList<Integer> listOfSums = new ArrayList<>();
            int Num1, Num2, AddedNum;
            String answer;
            do{
                System.out.println("Please enter a number");
                Num1 = input.nextInt();
                System.out.println("Please enter a second number");
                Num2 = input.nextInt();
                AddedNum = Num1 + Num2;
                System.out.println("The sum of the two entered numbers is " + AddedNum);

                listOfSums.add(AddedNum);

                System.out.println("Would you like to calculate the sum of two more numbers (y/n)?");
                answer = input.next();
            }
            while (answer.equals("y"));
            // Then you have to calculate the total sum at the end
            int totalSum = 0;
            for (int i = 0; i < listOfSums.size(); i++) 
            {
                totalSum = totalSum + listOfSums.get(0);
            }
            System.out.println("The total sum of all the numbers you entered is " + totalSum);
        }
    }

From what I see, you come from a background of C# (Since I see capital letter naming on all variables). Try to follow the java standards with naming and all, it will help you integrate into the community and make your code more comprehensible for Java devs.

There are several ways to implement what you want, I tried to explain the easiest. To learn more about ArrayList check this small tutorial.

Good luck!

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much, although I don't think I've ever used C# I have only really used Python and Lua
@MrPickles Then if you mean to learn Java, I would suggest following the naming conventions. Check this javatpoint.com/java-naming-conventions or google, there are a lot of resources available out there. Glad I helped, if you got your answer, you can mark the answer as accepted :) Good luck once again.
0

Solution with array:

import java.util.Scanner;
    public class StoredWhile{
        public static void main(String[] args) {
            Scanner input = new Scanner(System.in);
            int TotalNum[]=new int[10];
            int Num1, Num2, AddedNum;
            String answer;
            int count = 0;
            do{
                System.out.println("Please enter a number");
                Num1 = input.nextInt();
                System.out.println("Please enter a second number");
                Num2 = input.nextInt();
                AddedNum = Num1 + Num2;
                System.out.println("The sum of the two entered numbers is " + AddedNum);
    
                TotalNum[count]=AddedNum;
                count++;
                
    
                System.out.println("Would you like to calculate the sum of two more numbers (y/n)?");
                answer = input.next();
            }
            while (answer.equals("y"));
            int TotalSum = 0;
            for(int i = 0; i <count; i++ ) {
                TotalSum += TotalNum[i];
            }
            System.out.println("The total sum of all the numbers you entered is " + TotalSum);
        }
    }

This solution is not dynamic. There is risk that length of array defined on beginning will not be enough large.

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.