Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

initialized,but still initialization error

+1 vote
asked Apr 19 by GRENIN GAMER (130 points)

----------------------JAVA------------------------------------------------------

 

import java.util.Scanner;

class maxmin{

public static void main(String args[]){

Scanner scan=new Scanner(System.in);

System.out.println("Enter the size of array");
int size=scan.nextInt();

int arr[]=new int[size];
int max,min;
for(int a=0;a<arr.length;a++){

System.out.println("Enter element "+(a+1));
arr[a]=scan.nextInt();

if(a==0){
max=arr[a];
min=arr[a];
}

if(arr[a]>max){

max=arr[a];
}

if(arr[a]<min){

min=arr[a];
}
}
System.out.println("Maximum value= "+max);

System.out.println("Minimum value= "+min);

}

}

THE ERROR SAYS MY MIN AND MAX HAVENT BEEN INITIALIZED.

1 Answer

0 votes
answered Apr 20 by Peter Minarik (53,530 points)
edited Apr 20 by Peter Minarik

The compiler would not realize that your

if (a == 0)

check will set the values for min and max.

Just keep the compiler happy and assign some value to them at declaration time.

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

Your class should also be called Main as it has the main() function.

Your code correctly:

import java.util.Scanner;

class Main
{
    public static void main(String args[])
    {
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter the size of array");
        int size = scan.nextInt();
        int arr[] = new int[size];
        int max = Integer.MIN_VALUE;
        int min = Integer.MAX_VALUE;
        for (int a = 0; a < arr.length; a++)
        {
            System.out.println("Enter element " + (a + 1));
            arr[a] = scan.nextInt();
            if (a == 0)
            {
                max = arr[a];
                min = arr[a];
            }
            if (arr[a] > max)
            {
                max = arr[a];
            }
            if (arr[a] < min)
            {
                min = arr[a];
            }
        }
        System.out.println("Maximum value= " + max);
        System.out.println("Minimum value= " + min);
    }
}
commented Apr 21 by GRENIN GAMER (130 points)
I dont understand,It used to work when i used to do it with loops...
commented Apr 22 by Peter Minarik (53,530 points)
I'm not a Java expert, but there could be some compiler flags that would perhaps check for the branches -- if conditions -- to allow detection of such initializations (and most possibly slow down the compilation).

Or your code could have been different enough to not trip the compiler. ;)
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...