0

I am working on a coding project where I have to have a user input five specific float values. Then based on those values I have to to get a total, maximum, minimum and then apply interest. I am stuck right now on getting the minimum value from the array. I have been able to get the maximum value but when I print the minimum value I get 0.0. Any ideas?

import java.util.Scanner;

public class float_assignment {
    public static void main(String[] args) {
        float[] userNum = new float[5];
        float total = 0;
        float average = 0;
        float maximum = userNum[0];
        float minimum = userNum[0];
        float interest = 0;
        int i = 0;
        Scanner scnr= new Scanner(System.in);

        for (i = 0; i <= 4; ++i) {
            System.out.println("Please enter a number with a single decimal value:");
            userNum[i] = scnr.nextFloat();
        }

        for (i = 0; i < userNum.length; ++i) {
            if (userNum[i] > maximum) {
                maximum = userNum[i];
            }
        }

        for (i = 0; i < userNum.length; ++i) {
            if(userNum[i] < minimum) {
                minimum = userNum[i];
            }
        }

        total = userNum[0] + userNum[1] + userNum[2] + userNum [3] + userNum [4];

        System.out.println("");
        System.out.println("Total value is: " + total);
        System.out.println("");
        System.out.println("Maximum Vaule is: " + maximum);
        System.out.println("");
        System.out.println("Minimum Vaule is: " + minimum);
    }
}
0

5 Answers 5

2

The problem is with this

float[] userNum = new float[5];
.. //some other declarations
float minimum = userNum[0];

When you create an array of type float, all the elements are initialized to 0.0 (and it is obvious that you are inputting only positive numbers greater than 0)

See : java: primitive arrays — are they initialized?

To overcome this, initialize minimum (and maybe maximum too) after inputting the numbers from the console.

for(i = 0; i<=4; ++i) {
    System.out.println("Please enter a number with a single decimal value:");
userNum[i]= scnr.nextFloat();
}
minimum = maximum = userNum[0];
//Proceed to find max and min

Note that you don't need two loops to find min and max and can combine them into one.

for(i = 0; i < userNum.length; ++i ) {
    if(userNum[i]> maximum) {
        maximum = userNum[i];
    }
    if(userNum[i] < minimum) {
        minimum = userNum[i];
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much. This is my first project using loops and it had me stuck I could not figure it out. This worked great. Thank you again!
2

The issue is in the initial value of minimum. It's currently set at 0.

if(userNum[i] < minimum)

Will therefore never be true (assuming positive values). So you need to set the value of minimum to maximum just before you start the loop. Either that or set it to the max value allowed by float.

Comments

1

Java 8 version :

OptionalDouble min = IntStream.range(0, userNum.length).mapToDouble(i -> userNum[i]).min();
float minimum= (float) min.getAsDouble();

Comments

0

This is because in your initialization-
float userNum[] = new float[5];

Array userNum is by default being initialized with all zeros and it look like

userNum=[0.0,0.0,0.0,0.0,0.0]

And hence maximum and minimum are also initialized with 0.0

You might be entering all the positive values in the input that is why your maximum is showing correct but minimum remains 0.0

To avoid this initialize maximum and minimum with the first value of the array inside the for loop after it has been taken from the user.

Add this if statement inside your for loop of array assignmet

if(i==0)
        {
            maximum=minimum=userNum[0];
        }

Comments

0

Please note that by convention, it is advised to begin your class-name with a capital letter. :) Here, the logic you have applied is wrong. To make it work properly, you need to add another for loop inside your existing one. Then the value of variable 'minimum' must be updated during each iteration of your outer for loop and finally, place your if-condition inside inner for loop. Check the updated code given below:

for(i = 0; i< userNum.length; ++i) {
            minimum = userNum[0];     //updating value at each iteration.
            for(i = 0; i < userNum.length; ++i) {  //newly added loop.

                if(userNum[i] < minimum) {

                    minimum = userNum[i];
                }
            }
        }

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.