Search code examples
javainitializing

How to solve variable might not have been initialized error


I started studying for an exam and doing some practice programs with methods, and my mind is coming to a blank currently. I would like to know how I can initialize n1, n2, n3, and n4. I set them to 0 but the return statement returned only 0s.

public class LargestOfIntegers2
{
    public static int findLargest(int n1, int n2, int n3, int n4)
    {
        Scanner scan = new Scanner(System.in);
        System.out.print("Enter the first integer --> ");
        n1 = scan.nextInt();
        System.out.print("Enter the second integer --> ");
        n2 = scan.nextInt();    
        System.out.print("Enter the third integer --> ");
        n3 = scan.nextInt();    
        System.out.print("Enter the fourth integer --> ");
        n4 = scan.nextInt();    

        if(n1>n2 && n1 > n3 && n1 > n4)
            return n1;
        else if(n2 > n1 && n2 > n3 && n2 > n4)
           return n2;
        else if(n3>n1 && n3>n2 && n3>n4)
            return n3;
        else
            return n4;
    }

    public static void main(String[] args) {
        int n1, n2, n3, n4;

        findLargest(n1, n2, n3, n4);
        if(n1>n2 && n1 > n3 && n1 > n4)
            System.out.println("Out of the numbers " + n1 + ", " + n2 + ", " + n3 + ", " + n4 + ", the largest integer is " + n1);
        else if(n2 > n1 && n2 > n3 && n2 > n4)
            System.out.println("Out of the numbers " + n1 + ", " + n2 + ", " + n3 + ", " + n4 + ", the largest integer is " + n2);
        else if(n3>n1 && n3>n2 && n3>n4)
            System.out.println("Out of the numbers " + n1 + ", " + n2 + ", " + n3 + ", " + n4 + ", the largest integer is " + n3);
        else
            System.out.println("Out of the numbers " + n1 + ", " + n2 + ", " + n3 + ", " + n4 + ", the largest integer is " + n4);

    }
}

Solution

  • The variables n1 through n4 will be set in the findLargest function but only the local copies of them, the changes will never be "echoed" back to the main function. That's your primary problem since the variables in main are not being set because of that.

    You would be better off asking for each variable in a function and returning it, then using findLargest correctly by getting the return value. That would go something like:

    import java.util.Scanner;
    
    public class Test {
        public static int getNum(Scanner sc, String desc) {
            System.out.print("Enter the " + desc + " integer --> ");
            return sc.nextInt();
        }
    
        public static int findLargest(int n1, int n2, int n3, int n4) {
            if (n1 >= n2 && n1 >= n3 && n1 >= n4)
                return n1;
            if (n2 >= n3 && n2 >= n4)
               return n2;
            if (n3 >= n4)
                return n3;
            return n4;
        }
    
        public static void main(String[] args) {
            Scanner scan = new Scanner(System.in);
            int a = getNum(scan, "first");
            int b = getNum(scan, "second");
            int c = getNum(scan, "third");
            int d = getNum(scan, "fourth");
    
            int x = findLargest(a, b, c, d);
            System.out.println("max(" + a + "," + b + "," + c + "," + d + ") = " + x);
        }
    }
    

    You can see I've also made changes to the findLargest function to minimise comparisons. For example, if you get through the first if statement without returning, you know that n1 is irrelevant to further comparisons since it's smaller than at least one other value.