Home > Enterprise >  How to find min max values in Array - Scanner class
How to find min max values in Array - Scanner class

Time:12-03

I was trying to take user input, store the values in the Arrays, and then find min max values. My code works fine in case the array is initialized with values, but when I take user input it always returns 0 as min value. See the code:

import java.util.Arrays;
import java.util.Scanner;

public class Loops {
    public static void main(String[] args) {
    

    Scanner scan = new Scanner(System.in);
    System.out.print("How many numbers in a set: ");
    int number = scan.nextInt();
    int[] myArray = new int[number];
    int min = myArray[0];
    int max = myArray[0];
    
        
    System.out.println("Input your numbers: ");
    for(int i = 0; i <= myArray.length-1; i  ) {
        myArray[i] = scan.nextInt();
        if (myArray[i] < min) {
            min = myArray[i];
        } else if (myArray[i] > max) {
        max = myArray[i];
        }
    }
        System.out.println(Arrays.toString(myArray));
        System.out.println(max   " "   min);
        System.out.println();
    }
}

As a beginner, I'd appreciate your look at the above piece of code and hints

CodePudding user response:

When you did this

int[] myArray = new int[number];
int min = myArray[0];
int max = myArray[0];

you set min and max on 0 and then, in for loop, you check if it is less than 0. If you are giving only positive numbers to your program,min value will stay 0 If you want to find min and max value of array you can initialize min and max like this:

int min = Integer.MAX_VALUE
int max = Integer.MIN_VALUE

CodePudding user response:

Your code initializes min as 0 by default which is smaller most of the numbers we give in the array

int min = Integer.MAX_VALUE; //maximum value Integer datatype can hold
int max = Integer.MIN_VALUE;// minimum value Integer datatype can hold

Here we make min as the heighest possible number and the max as the lowest possible number which makes the code mode bullet-proof to -ve values as well!. to know more about Integer.MAX_VALUE and Integer.MIN_VALUE follow this link: https://www.geeksforgeeks.org/integer-max_value-and-integer-min_value-in-java-with-examples/

CodePudding user response:

public static void main(String... args) {           
    Scanner scan = new Scanner(System.in);          
    System.out.print("How many numbers in a set: ");
    
    int[] arr = new int[scan.nextInt()];                                                            
    int min = Integer.MAX_VALUE;                    
    int max = Integer.MIN_VALUE;                    
                                                    
    System.out.print("Input your numbers: ");       
                                                    
    for (int i = 0; i < arr.length; i  ) {          
        arr[i] = scan.nextInt();                    
        min = Math.min(min, arr[i]);                
        max = Math.max(max, arr[i]);                
    }                                               
                                                    
    System.out.println(Arrays.toString(arr));       
    System.out.println(max   " "   min);            
    System.out.println();                           
}                                                   
  • Related