0

I'm trying to get the minimum and maximum values from an array of doubles. The maximum value works fine, however, the minimum value always seems to be zero. What would be the best way to get the minimum value? Please note that I cannot use a for loop in this exercise, but a while loop.

public class LoopingFloats {

    public static void main(String[] args) {
        double[] inputHolder = new double[5];
        int inputCounter = 0;
        double total = 0.0d;
        double average = 0.0d;
        double maximum = 0.0d;
        double minimum = inputHolder[inputCounter];
        double interestRate = 0.20d;
        double interestAmount = 0.0d;

        Scanner scnr = new Scanner(System.in);

        while(inputCounter <= 4){
            System.out.println("Enter number " + (inputCounter + 1) + ": ");
            inputHolder[inputCounter] = scnr.nextDouble();

            if(inputHolder[inputCounter] >  maximum){
                maximum = inputHolder[inputCounter];
            }
            if(inputHolder[inputCounter] <  minimum){
                minimum = inputHolder[inputCounter];
            }

            total = total + inputHolder[inputCounter];

            inputCounter += 1;

        }

    }

        average = total / 5;
        interestAmount = total * interestRate;

        System.out.println("Total: " + total);
        System.out.println("Average: " + average);
        System.out.println("Maximum: " + maximum);
        System.out.println("Minimum: " + minimum);
        System.out.println("Interest for total at 20%: " + interestAmount);
    }

}
4
  • 1
    You're initializing the minimum with a value of 0.0 (default value for elements of a double[] array. Commented May 14, 2019 at 4:41
  • the following double minimum = inputHolder[inputCounter]; initializes minimum to 0.0 by default. You should assign double minimum = Double.MAX_VALUE Commented May 14, 2019 at 4:42
  • @akshayapandey Forgot a minus sign there. Commented May 14, 2019 at 4:42
  • no we wish to assign minimum variable to the max value initially, so when it finds a smaller double values, it can assign it to minimum. Commented May 14, 2019 at 4:44

3 Answers 3

1

The problem is in this line:

double minimum = inputHolder[inputCounter];

Java initialises new variables, and all elements of a new array, to 0 values. See here.

So by default your minimum variable is set to 0 and if your array has values greater than 0, your minimum variable won't be updated because 0 is less than all other positive values.

What you'll have to do is set it to a very large value, such as 99999, or you can use Double.MAX_VALUE which will give you the maximum value a double can store.

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

Comments

0

I would use DoubleSummaryStatistics which includes all of your required information (I would also prefer formatted IO). Like

double[] inputHolder = new double[5];
double interestRate = 0.20;
Scanner scnr = new Scanner(System.in);
int i = 0;
while (i < 5) {
    System.out.printf("Enter number %d: ", i + 1);
    inputHolder[i] = scnr.nextDouble();
    i++;
}
DoubleSummaryStatistics dss = Arrays.stream(inputHolder).summaryStatistics();
System.out.printf("Total: %.2f%n", dss.getSum());
System.out.printf("Average: %.2f%n", dss.getAverage());
System.out.printf("Maximum: %.2f%n", dss.getMax());
System.out.printf("Minimum: %.2f%n", dss.getMin());
System.out.printf("Interest for total at 20%%: %.2f%n", dss.getSum() * interestRate);

Comments

0

Everything seems ok excepts initialization of variableminimum. Initialize it in while loop after the inputHolder[inputCounter] = scnr.nextDouble();statement

inputHolder[inputCounter] = scnr.nextDouble();
if (inputCounter == 0) minimum = inputHolder[inputCounter];

Hope this will solve your problem.

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.