1

My problem is that program is not reading codes as i intended "he" would.

I have

if (hero.getPos() == (6 | 11 | 16)) {
    move = new Object[] {"Up", "Right", "Left"};
} else {
    move = new Object[] {"Up", "Down", "Right", "Left"};
}

When hero position is 6, the program still goes to else.

Why is that? Is it because of operands? If yes, how should i change it?

Hans
  • 752
  • 1
  • 15
  • 29

12 Answers12

11

Use:

if (hero.getPos() == 6 || hero.getPos() == 11 || hero.getPos() == 16)) {

This will do what you want.

What you did is comparing hero.getPos() with the result of (6|11|16) which will do bitwise or between those numbers.

BobTheBuilder
  • 18,858
  • 6
  • 40
  • 61
4

The other answers are correct, just thinking differently you may use Sets.

static final Set<Integer> positions = new HashSet<Integer>();
static{
    positions.add(6);
    positions.add(11);
    positions.add(16);
}

if (positions.contains(hero.getPos())){
    move = new Object[] {"Up", "Right", "Left"};
} else {
    move = new Object[] {"Up", "Down", "Right", "Left"};
}
Serkan Arıkuşu
  • 5,549
  • 5
  • 33
  • 50
2

You cannot do it like that. It ors the 3 number bitwise.

You have to do like this :

if (hero.getPos() == 6 || hero.getPos() == 11 | hero.getPos() == 16)) {
    move = new Object[] {"Up", "Right", "Left"};
} else {
    move = new Object[] {"Up", "Down", "Right", "Left"};
}

You see the difference ? | is a bitwise or while || is a logical or. Note also that you have to rewrite the comparison each time.

Hugo Dozois
  • 8,147
  • 12
  • 54
  • 58
2

(6 | 11 | 16) would be evaluated first to 31 (binary operation), which is 6 != 31. Not what you want.

Better is to check every single position (you have only 3, so inline is good, for more consider using a loop):

if (hero.getPos() == 6 || hero.getPos() == 11 | hero.getPos() == 16)) {
    move = new Object[] {"Up", "Right", "Left"};
} else {
    move = new Object[] {"Up", "Down", "Right", "Left"};
}
Johannes Kuhn
  • 14,778
  • 4
  • 49
  • 73
1

No, you're going to need to check ci.getNumber() == ... for each value, or add them to a collection and check myCollection.contains(ci.getNumber()). However, you may want to re-think the structure of your code if you are checking a method against several known values.

Jim Mitchener
  • 8,835
  • 7
  • 40
  • 56
  • FYI: this answer merged from http://stackoverflow.com/questions/2001691/java-operators-compare-multiple-values – Shog9 Jul 24 '14 at 19:06
1

using the answer from:

How can I test if an array contains a certain value?

you could create an array of numbers and check if your ci.getNumber() is in it.

Community
  • 1
  • 1
John Boker
  • 82,559
  • 17
  • 97
  • 130
  • FYI: this answer merged from http://stackoverflow.com/questions/2001691/java-operators-compare-multiple-values – Shog9 Jul 24 '14 at 19:06
1

There is no such operator. But if you are comparing number, you can use switch do simulate that. Here is how:

int aNumber = ci.getNumber();
swithc(aNumber) {
    case 6252001:
    case 5855797:
    case 6251999: {
        ...
        break;
    }
    default: {
        ... // Do else.
    }
}

Hope this helps.

NawaMan
  • 25,129
  • 10
  • 51
  • 77
  • FYI: this answer merged from http://stackoverflow.com/questions/2001691/java-operators-compare-multiple-values – Shog9 Jul 24 '14 at 19:06
1

No. You could create a Set<Integer> once and then use that, or just:

int number = ci.getNumber();
if (number == 6252001 || number == 5855797 || number == 6251999)

I'd also consider changing those numbers into constants so that you get more meaningful code.

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • FYI: this answer merged from http://stackoverflow.com/questions/2001691/java-operators-compare-multiple-values – Shog9 Jul 24 '14 at 19:06
1
boolean theyAretheSame = num1 == num2 ? (num1 == num3 ? true:false):false;

I must admit I haven't checked this but the logic looks correct.

Aurelio De Rosa
  • 21,856
  • 8
  • 48
  • 71
Ross
  • 11
  • 1
  • FYI: this answer merged from http://stackoverflow.com/questions/2001691/java-operators-compare-multiple-values – Shog9 Jul 24 '14 at 19:06
0

You could put all the numbers in a collection, and then use the contains() method. Other than that, I don't believe there is any special syntax for comparing like you want to do.

avpx
  • 1,892
  • 15
  • 13
  • FYI: this answer merged from http://stackoverflow.com/questions/2001691/java-operators-compare-multiple-values – Shog9 Jul 24 '14 at 19:06
0

Java won't let you do that. You can do a hash lookup (which is overkill for this) or a case statement, or a big honking ugly multiple compare:

if ((n==1 ) || (n==2) || ...
Steve B.
  • 55,454
  • 12
  • 93
  • 132
  • FYI: this answer merged from http://stackoverflow.com/questions/2001691/java-operators-compare-multiple-values – Shog9 Jul 24 '14 at 19:06
0

no.. you have to compare them individually.

GuruKulki
  • 25,776
  • 50
  • 140
  • 201
  • FYI: this answer merged from http://stackoverflow.com/questions/2001691/java-operators-compare-multiple-values – Shog9 Jul 24 '14 at 19:07