user3552172
user3552172

Reputation: 23

Getting minimum value from double array not working

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);
    }

}

Upvotes: 0

Views: 76

Answers (3)

Swapnil Patil
Swapnil Patil

Reputation: 613

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.

Upvotes: 0

vixrant
vixrant

Reputation: 198

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.

Upvotes: 1

Elliott Frisch
Elliott Frisch

Reputation: 201419

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);

Upvotes: 0

Related Questions