-3

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);

    }
}
jruizaranguren
  • 12,679
  • 7
  • 55
  • 73
  • If you are not using `n1, .., n4` original values, why do you pass them as parameters? Shouldn't they be local variables? – Daniel Dec 11 '14 at 01:45
  • `I set them to 0` - where? I don't see it... – August Dec 11 '14 at 01:46
  • You seem to be expecting to get all four values back from the method. But Java doesn't work that way - it's "pass by value", not "pass by reference". If you need to get all four values back from the method, one option is to return an array. You might want to have one method that just fetches the values from the user, and a separate method which figures out the largest one. – Dawood ibn Kareem Dec 11 '14 at 01:46
  • you never call findLargestInt from main – Ellery Dec 11 '14 at 01:48
  • You seem to be confused about the two variables called `n1` in your code. One of those variables is an argument of `findLargest` (and that is the variable you initialize). But the other is a local variable called `n1` in your `main`, you cannot use that variable until you have initialized it. That is why you get that error. Remove those arguments in your function as they make no sense as for my first comment. – Daniel Dec 11 '14 at 01:49

3 Answers3

0

The main reason your code didn't work:

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

    findLargest(n1, n2, n3, n4);    <--- YOU DID NOT STORE THE RETURNED VALUE
    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);

}

Do either:

//prompt user input for n1, n2, n3, n4 first
System.out.println(findLargest(n1,n2,n3,4));

OR

//prompt user input for n1, n2, n3, n4 first
int largest = findLargest(n1,n2,n3,n4);
System.ouot.println(largest);

I am sure this will make your code work. Of course, instead of prompting the user input in the method, you should prompt it outside the method, before you call the method.


Explanation of how Java method passes its value:

public static void main(String[] args)
{
    int n = 100;
    someMethod(n);
    System.out.println(n); //n will still be 100;
}

public static void someMethod(int n1)
{
    n1 = 999;
}

Why is n in the main still 100, and not 999. This is because Java passes everything by value. For primitive types, the value passed is the actual value itself. Meaning that a copy of the actual value will be passed in to the method. A copy was made. Thus n1(scope:someMethod) is just a copy of n(scope:main). Changes on n1 do not affect n.


Something extra for you:

If you are passing in the 4 variables n1, n2, n3, n4. Why are you asking the user to input the 4 values once more in your method?

Doing so will "void" the values of n1, n2, n3, n4 passed in from the caller of the method.

By the way there are many much simpler ways to implement this. By not touching on arrays, you can simply do this:

public static int findLargest(int n1, int n2, int n3, int n4)
{
    return Math.max(Math.max(n1,n2), Math.max(n3,n4));
}
user3437460
  • 17,253
  • 15
  • 58
  • 106
  • No, you've still missed the key point. The `main` method tries to use those variables without initialising them first, and that's what the compile error is saying. – Dawood ibn Kareem Dec 11 '14 at 03:02
  • @DavidWallace Well, I edited it. User should be prompted for all 4 values before calling the method. – user3437460 Dec 11 '14 at 03:18
  • It's a little better; but you still haven't really explained why Alex is getting the compile error. This is a very long answer, considering the fact that it only touches very briefly on the actual issue. – Dawood ibn Kareem Dec 11 '14 at 03:25
  • @DavidWallace You want to edit my post and come out with a answer together? There are more than 1 area having problem in Alex's code. If I give a totally different solution, I am actually not answer his question, If I try to address each problem individually, it will confuse OP more. Maybe you want to edit my post if there is still something lacking. – user3437460 Dec 11 '14 at 03:31
  • OK, maybe I'll do it later, since you've given me permission. Alternatively, I guess, I could post an answer of my own. I'll see how much time I have later today. – Dawood ibn Kareem Dec 11 '14 at 03:43
0

this is what your code will do: this will get new n1 and n2 and n3 and n4 and set them equal to the values you pass

  public static int findLargest(int n1, int n2, int n3, int n4){
 {

you pass an uninitialized value so they are uninitialized you then initialize them with the user input (THIS WONT CHANGE ANYTHING OUTSIDE OF THIS METHOD AS YOUR VARIABLES ARE LOCAL) you then find the biggest and return it YOUR CODE DOESN'T STORE THIS VALUE ANYWHERE

 int thelargest=findLargest(n1, n2, n3, n4);

would store it

you then try and compare UNINITIALIZED INTS as the method findlargest can't see n1 n2 n3 n4 as there is no pass by refference to fix your code to do what i think you want do this

 static int n1,n2,n3,n4;
  public static void findLargest()

{
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();    
//nothing is listening for these numbers anyway why bother with it
 // 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();
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);

}

}
0

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.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • Hey, I don't know if you'll see this but your explanation helped the most. Idk what happened, but something just clicked in my head lol. Thanks a lot again. – Alex McMahon Dec 11 '14 at 03:48