1

I am trying to create a "calculator", except it needs to print each element that was used to compose the sum total. This printing of the array elements needs to happen at the end of the program, when the user inputs 0 twice in a row.

Upon entering an input, the integer values will be stored in an array. Once the end of the program has been reached, the contents of this array will be printed. However, if the end of the program has not been reached, the program continues while the user adds consecutive inputs.

Currently, the program will only print one element at a time, instead of every element that was used to calculate the total. I've spent hours trying to debug, and any guidance would be greatly appreciated!

import java.util.*;

public class AddingMachine {

public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    boolean justStarting = true;
    int total = 0;
    int subtotal = 0;
    int input;
    int last = 1;
    int MAXIMUM_NUMBER_OF_INPUTS = 100;
    while (true) {
        input = scanner.nextInt();
        if (input == 0) {
            if (last == 0) {
                System.out.println("total " + total);
                return;
            }
            System.out.println("subtotal " + subtotal);
            total += subtotal;
            subtotal = 0;
        }
        subtotal += input;
        last = input;
        int[] numbers = new int[args.length];
        for (int i = 0; i < args.length; i++) {
            numbers[i] = last;
        }
        System.out.println(Arrays.toString(numbers));
    }
}
2
  • Anytime you are writing a command line program, you should let the user know what he/she needs to do. Example: System.out.print("Please enter a number: "); Example: System.out.println("Please press z to end the program"); Commented Mar 1, 2017 at 21:54
  • Possible duplicate of Java reading multiple ints from a single line Commented Mar 1, 2017 at 21:57

2 Answers 2

0

When summing the input in your loop, you could store the users input into a List of integers. Once you need to reprint them, you can iterate over the List and print the elements you stored.

Example:

List<Integer> storedUserInput = new ArrayList<>();
while (true) {
        input = scanner.nextInt();
        storedUserInput.add(input);
    if (input == 0) {
        if (last == 0) {
            for(Integer i : storedUserInput){
                 System.out.print(i + " + ");
            }
            System.out.println("total " + total);
            return;
        }
        System.out.println("subtotal " + subtotal);
        total += subtotal;
        subtotal = 0;
    }

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

Comments

0

Within the while loop, the array is re-initialized each time:

int[] numbers = new int[args.length];

so any previously entered value is lost. Also, the purpose of the for loop within the while is not clear.

Also, unless using an array is a requirement, you really don't need an array. You could just use a StringBuffer and append the entered values.

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.