0

This is the specific requirements for the assignment:

"First, launch NetBeans and close any previous projects that may be open (at the top menu go to File ==> Close All Projects).

Then create a new Java application called "MinMax" (without the quotation marks) that declares an array of doubles of length 5, and uses methods to populate the array with user input from the command line and to print out the max (highest) and min (lowest) values in the array. The methods that determine the max and min values may not use any built-in sort methods in Java. That is, you need to write the appropriate logic in those methods.

NOTE: For this assignment and all future assignments that deal with methods, you should be calling the appropriate method to do the task indicated, rather than implementing the task logic in the main method itself."

Specifically, I'm having trouble converting the int arrays in the getMin and getMax methods to the double array in the main method.

package minmax;
import java.util.Scanner;

public class MinMax {

    public static void main(String[] args) {
        Scanner scnr = new Scanner(System.in);
        double[] userVals = new double[5];
        double userNumbers;

        System.out.print("Enter 5 numbers: ");
        userNumbers = scnr.nextDouble();

        System.out.print("Minimum number: ");

    }

    public static int getMin(int[] array) {
        int minNum = array[0];

        for (int i = 0; i < array.length; ++i) {
            if (array[i] < minNum) {
                minNum = array[i];
            }
        }
        return minNum;
    }

    public static int getMax(int[] array) {
        int maxNum = array[0];

        for (int i = 0; i < array.length; ++i) {
            if (array[i] > maxNum) {
                maxNum = array[i];
            }
        }
        return maxNum;
    }

}

I'm trying to print out both the min. and max. of the 5 user numbers. I know that my code is incomplete in the main method, but that's due to not knowing how to convert int arrays to double arrays.

5
  • 1
    Why don't you use a double array in the getMin and getMax function? The assignment text you provided doesn't ask for int arrays. Commented Nov 11, 2019 at 21:05
  • I changed that after I realized that I never changed the modifier from int to double. I kept getting the "lossy" error. So that's fixed, thank you. But now I'm just getting a zero for the output. Commented Nov 11, 2019 at 21:17
  • You never actually put data into your userVals array. Do something like for(int i = 0; i < 5; i++) userVals[i] = scnr.nextDouble(); and maybe add your output so the user knows he has to enter the numbers. Commented Nov 11, 2019 at 21:21
  • Does this answer your question? Java Minimum and Maximum values in Array Commented Nov 12, 2019 at 4:42
  • Unfortunately, it does not. The assignment requires an array of doubles, and I keep getting errors if I try mixing ints with doubles. I don't know if it's me not writing the code correctly, or if I need to somehow convert. But I know I don't know enough to know. Commented Nov 14, 2019 at 22:06

1 Answer 1

0

to summarize:

 public static void main(String[] args) {
    Scanner scnr = new Scanner(System.in);
    double[] userVals = new double[5];

    System.out.print("Enter 5 numbers: ");
    for(int i=0;i< 5;i++){
        userVals[i]= scnr.nextDouble();
    }

    System.out.print("Minimum number: "+ getMin(userVals));

}

 public static double getMin(double[] array) {
    double minNum = array[0];

    for (int i = 0; i < array.length; ++i) {
        if (array[i] < minNum) {
            minNum = array[i];
        }
    }
    return minNum;
}
Sign up to request clarification or add additional context in comments.

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.