-1

i'll start by saying im quite new to Java, ive got reasonable experience in python but Java is fairly new to me.

My problem is that I have created a method to make sure 4 random numbers are all unique to one another, ive done this by just comparing every possible combination using while loops.

I can see that my code works

public static void EqualValues(int num1, int num2, int num3, int num4) {
    Random ran = new Random();
    while(num1 == num2 || num1 == num3 || num1 == num4) {
        num1 = ran.nextInt(9);
        num1 += 1;
    }
    while(num2 == num1 || num2 == num3 || num2 == num4) {
        num2 = ran.nextInt(9);
        num2 += 1;
    }

    while(num3 == num1 || num3 == num2 || num3 == num4) {
        num3 = ran.nextInt(9);
        num3 += 1;
    }
    while (num4 == num1 || num4 == num2 || num4 == num3) {
        num4 = ran.nextInt(9);
        num4 += 1;
    }

    System.out.println(num1);
    System.out.println(num2);
    System.out.println(num3);
    System.out.println(num4);
    System.out.println(".........");
}

Which will output 4 unique numbers ie: 2 6 8 4

However, when I do the same in my main method:

public static void main(String[] args) {

    Random ran = new Random();

    int num1 = ran.nextInt(9);
    int num2 = ran.nextInt(9);
    int num3 = ran.nextInt(9);
    int num4 = ran.nextInt(9);

    num1 += 1;
    num2 += 1;
    num3 += 1;
    num4 += 1;

    EqualValues(num1, num2, num3, num4);

    System.out.println(num1);
    System.out.println(num2);
    System.out.println(num3);
    System.out.println(num4);
}

I get what seems to be the four original numbers before they were checked ie: 2 6 4 4

If someone could point out where i have gone wrong. I hope i have described my question so its easy to understand.

Rahul Agarwal
  • 4,034
  • 7
  • 27
  • 51
HarveyR
  • 21
  • 1
  • 3
  • you just change the values of the local variables in that method. – Stultuske Feb 22 '19 at 11:29
  • It's because your method is receiving "copies" of your variables, therefore the originals are not modified – Ayrton Feb 22 '19 at 11:29
  • 3
    See [Is Java “pass-by-reference” or “pass-by-value”?](https://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value) – khelwood Feb 22 '19 at 11:30
  • Ok i see, could you explain how exactly I go about fixing that please? – HarveyR Feb 22 '19 at 11:32
  • either you return those values to the main method, or you declare the variables on class level, and don't pass them as parameters to the method – Stultuske Feb 22 '19 at 11:34

1 Answers1

0

What happens is the following: when you pass your variables to EqualValues (which you should rename to equalValues to follow convention) it actually receives "copies" of them, which it cannot modify. There are two ways to work around that:

1 - You return the modified values, possibly in an array.

2 - You don't pass any parameters, and instead declare your variables as static and let the method access them directly.

However, I'd propose another approach altogether: pass a Set object to your method. That way, the method will modify only the contents of your Set, and it would also handle repetitions for you (since Sets only allow unique values) so that your code would be much cleaner.

Example:

private static final Random RNG = new Random();

private static void fillWithRandoms(Set<Integer> set) {
    while(set.size() < 4) {
        int rando = RNG.nextInt(9) + 1;
        set.add(rando);
    }
}

public static void main(String[] args) {
    Set<Integer> set = new HashSet<Integer>();

    fillWithRandoms(set);

    System.out.println(set);
}
Ayrton
  • 2,218
  • 1
  • 14
  • 25
  • Thankyou! This is very helpful thought maybe a touch confusing right now. I'll have a look at using sets, i've never come accross them but if each valuue is totally unique as you say, they would be ideal. Cheers – HarveyR Feb 22 '19 at 11:49
  • If you have further questions, make sure to look for them here. If no one's asked it yet you're free to ask them yourself. Also, I'd recommend you take a look at Oracle's Java tutorial: https://docs.oracle.com/javase/tutorial/index.html. It's very thorough, and also has a section on the Collections API (which includes the `Set` interface). – Ayrton Feb 22 '19 at 11:57