0

i am currently working on a java code that takes input from user and outputs the size of arraylist, the sum of numbers entered, the average and the maximum number entered. i have not been able to complete the sum as the for loop is not calculating result. I would appreciate any suggestions. my code is as follows.

import java.util.ArrayList;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        // write your code here
        double size = 0;
        double a = -999;
        double total = 0;

        Scanner in = new Scanner(System.in); // scanner object for user input

        ArrayList<Double> inputs = new ArrayList<Double>();
        System.out.println("Enter a number, to terminate enter -999 :");
        while (in.hasNextDouble()) {
            //assign the nextDouble to a variable
            double tmp = in.nextDouble();
            //test the variable
            if (tmp != a) {
                //add the variable
                //if you want to add -999 to the inputs then this next line above the test.
                inputs.add(tmp);
                System.out.println("Enter a number, to terminate enter -999 :");
            } else {
                inputs.size(); // get size of input
                System.out.println("Numbers entered: " + inputs.size());
                break;
            }
        }
        for (double sum : inputs) {
            total += sum;
            System.out.println("The sum of numbers entered is " + total);
            break;
        }

    }
}
0

2 Answers 2

3

The break in the for loop causes the loop to exit. Here, it's causing the loop to exit on the first iteration, which is almost certainly not what you want.

Move the println outside the loop, and remove the break:

for (double sum : inputs) {
    total += sum;
}

System.out.println("The sum of numbers entered is " + total);

This allows the for loop to iterate the entire list while calculating the sum so you can print it after instead of exiting prematurely.


Also note that this:

inputs.size(); // get size of input

Isn't doing anything useful. size returns the size of inputs, then you don't do anything with that number, so it's lost. You should just remove that line since you call size again on the next line anyways.

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

Comments

0

The problem you have in your code is you have a break condition in your for loop

The correct code is the following

import java.util.ArrayList;
import java.util.Scanner;

public class Main {

public static void main(String[] args) {
    // write your code here
    double size = 0;
    double a = -999;
    double total = 0;

    Scanner in = new Scanner(System.in); // scanner object for user input

    ArrayList<Double> inputs = new ArrayList<Double>();
    System.out.println("Enter a number, to terminate enter -999 :");
    while (in.hasNextDouble()) {
        //assign the nextDouble to a variable
        double tmp = in.nextDouble();
        //test the variable
        if (tmp != a) {
            //add the variable
            //if you want to add -999 to the inputs then this next line above the test.
            inputs.add(tmp);
            System.out.println("Enter a number, to terminate enter -999 :");
        } else {
            inputs.size(); // get size of input
            System.out.println("Numbers entered: " + inputs.size());
            break;
        }
    }
    for (double sum : inputs) {
        total += sum;
    }

    System.out.println("The sum of numbers entered is " + total);

}
}

2 Comments

hi would you have any advise of how to find the largest number entered by user?#
Only store it in a local variable in the while block

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.