views:

342

answers:

7

Is there any way I can compare values like this?

if (ci.getNumber() == (6252001|5855797|6251999)) { ... }
A: 

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  A: 

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.

jcm
A: 

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.
+1  A: 

using the answer from:

http://stackoverflow.com/questions/1128723/in-java-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.

John Boker
+1  A: 

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
don't forget the `break`s ...
Carlos Heuberger
Hey! Thanks :-D
NawaMan
A: 

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.

Jon Skeet
A: 

no.. you have to compare them individually.

GK