-5

For instance, would this program run:

*loop code.......* 
if (variable1 != variable2 != variable3 != variable4){break loop;}
e4a
  • 17
  • 2
  • 8
  • However, then it is comparing variable 2 to variable 4 for instance. I want to make sure none of those variables equal any of the other variables – e4a Aug 03 '17 at 23:31

3 Answers3

4

What you're really trying to test is whether there are any duplicate values in your set of variables. Here's a simple method to test that:

static boolean allUnique(Object... values) {
    return new HashSet<>(Arrays.asList(values)).size() == values.length;
}

You can call it like this:

if (allUnique(variable1, variable2, variable3, variable4)) break loop;
shmosel
  • 49,289
  • 6
  • 73
  • 138
0
 if (new HashSet(variable1, variable2, variable3, variable4).size == 4) ...
Dima
  • 39,570
  • 6
  • 44
  • 70
-2

Yes, you need to use the logical && AND operator. More info in this SO answer.

Essentially you will use something like:

if (variable1 != variable2 && variable1 != variable3 && variable1 != variable4)

This will check that variable 1 is not equal to 2, 3 or 4.

twoleggedhorse
  • 4,938
  • 4
  • 23
  • 38
  • However, then it is comparing variable 2 to variable 4 for instance. I want to make sure none of those variables equal any of the other variables – e4a Aug 03 '17 at 23:32
  • Please give a better example, what are you trying to do in words? Check variable 1 is not equal to 2,3 and 4? Or that none of the variables are the same? – twoleggedhorse Aug 03 '17 at 23:35
  • Sorry my fault, I am trying to make sure that none are the same. Specifically, the program that I'm working on is generating 4 random integers out of 1,000 and I am trying to ensure that none of those ten are the same. – e4a Aug 03 '17 at 23:36
  • 1
    In that case I would use a hashset as others have already demonstrated. – twoleggedhorse Aug 03 '17 at 23:38