-1

I want to get the minimum value and maximum value inside an array. After the user inputs n number of arrays, it will print the minimum and max value inside that array.

Here is an example output

enter image description here

Here is my code so far

public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        try {
            System.out.println("Enter array size: ");
            int n = input.nextInt();

            int [] array = new int[n];
            int max = getMaxValue(array);
            int min = getMinValue(array);

            System.out.println("Enter " + n + " elements:");
            for (int i = 1; i <= array.length; i++) {
                array[i] = input.nextInt();
            }

            System.out.println("Max Value: " + max);
            System.out.println("Min Value: " + min);

        } catch (InputMismatchException e) {
            System.out.println("INVALID INPUT >> PLEASE INPUT A NUMBER");
        }
    }

    private static int getMaxValue(int[] array) {
        int maxValue = array[0];
        for (int i = 1; i < array.length; i++){
            if (array[i] > maxValue){
                maxValue = array[i];
            }
        }
        return maxValue;
    }

    private static int getMinValue(int[] array) {
        int minValue = array[0];
        for (int i = 1; i < array.length; i++){
            if (array[i] < minValue) {
                minValue = array[i];
            }
        }
        return minValue;
    }
}
WiloWisk
  • 137
  • 3
  • 10
  • And what is the problem? – camickr Sep 25 '20 at 03:24
  • You have to calculate the maximum and minimum value after you ask the user for input, not before. – Grumblesaurus Sep 25 '20 at 03:26
  • You should also put the try/catch closer to the input. The way you have it right now, it breaks the loop if they input badly formed text. – Grumblesaurus Sep 25 '20 at 03:27
  • when I run the program, it doesn't show the max and min value in the array. For example, after you input the n numbers of array, the program ends after that but doesn't show the min and max value – WiloWisk Sep 25 '20 at 03:28
  • *it doesn't show the max and min value in the array* - because the Array contains no values when you invoke your min/max methods. – camickr Sep 25 '20 at 03:32

3 Answers3

0

There are a few problems keeping this program from working:

  1. getMinValue and getMaxValue should be called after your input loop
  2. All 3 of your loops index starting from 1, they should start from 0
  3. The input loop repeats until i <= array.length, but it should end at i < array.length

Problem 1 is that these method are called on the empty array. Problems 2 and 3 will cause an ArrayIndexOutOfBoundsException.

Here's how the corrections would look in your code:

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        try {
            System.out.println("Enter array size: ");
            int n = input.nextInt();

            int [] array = new int[n];

            System.out.println("Enter " + n + " elements:");
            for (int i = 0; i < array.length; i++) {
                array[i] = input.nextInt();
            }
            int max = getMaxValue(array);
            int min = getMinValue(array);

            System.out.println("Max Value: " + max);
            System.out.println("Min Value: " + min);

        } catch (InputMismatchException e) {
            System.out.println("INVALID INPUT >> PLEASE INPUT A NUMBER");
        }
    }

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

    private static int getMinValue(int[] array) {
        int minValue = array[0];
        for (int i = 0; i < array.length; i++){
            if (array[i] < minValue) {
                minValue = array[i];
            }
        }
        return minValue;
    }
}
tune5ths
  • 676
  • 6
  • 18
0

There were 3 problems in your code which are as follows:

  1. int max = getMaxValue(array) int min = getMinValue(array) are called in wrong places

when we initialize an int array the default value present in it is0. So when you had call getMaxValue().
array[0] = 0 is compared with all rest of the values in the array (which are 0) and from all 0 is the maximum.
Same goes for getMinValue().

  1. You have called the function getMaxValue() and getMinValue() without filling the array,with user input. you should have used a for loop from 0 to array.length.
    for(int index = 0 ; index < array.length ; index++){
       array[index] = input.nextInt();
    }
  1. Has you would have noticed, I have index staring from 0 to array size. what you did:
for (int i = 1; i <= array.length; i++) {
   array[i] = input.nextInt();
 }

this will cause ArrayIndexOutOfBoundException Because array indexing starts from 0 to n-1 and you allowed it to run till 1 to n.

Additionally, the finding of min and max can be done in one function. by keeping the return type as an array.

public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        try {
            System.out.println("Enter array size: ");
            int n = input.nextInt();

            int [] array = new int[n];
            
            System.out.println("Enter " + n + " elements:"); // <-- changed
            for (int i = 0; i < array.length; i++) {
                array[i] = input.nextInt();
            }
            
            int max_min[] = getMax_MinValue(array); // <- notice this.
            
            System.out.println("Max Value: " + max_min[0]);
            System.out.println("Min Value: " + max_min[1]);

        } catch (InputMismatchException e) {
            System.out.println("INVALID INPUT >> PLEASE INPUT A NUMBER");
        }finally {
            input.close(); // close the scanner 
        }
    }

    // return type changed 
    private static int[] getMax_MinValue(int[] array) {
       
        int maxValue = array[0];
        int minValue = array[0];
       
        for (int i = 0; i < array.length; i++){

            if (array[i] > maxValue){
                maxValue = array[i];
            }
            if(array[i] < minValue) {
                minValue = array[i];
            }

        }
        
        int min_max[] = {maxValue,minValue}; // min_max[0] has max value and min_max[1] has min value
        return min_max;
    }

Output:

Enter array size: 
8
Enter 8 elements:
5
23
9
14
45
36
42
39
Max Value: 45
Min Value: 5
Dharman
  • 30,962
  • 25
  • 85
  • 135
Swapnil Padaya
  • 687
  • 5
  • 14
0

This is not an appropriate answer.

However as the implementations of getMaxValue & getMinValue have a slight flaw: empty arrays will raise an ArrayIndexOutOfBoundException.

And especially one might use the Stream classes.

private static int getMaxValue(int[] array) {
    return IntStream.of(array).max().orElse(0);
}

private static int getMinValue(int[] array) {
    return IntStream.of(array).min().orElse(0);
}

max and min yield an OptionalInt, and for empty arrays one may give a default (orElse).

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138