Nicolas Martel
Nicolas Martel

Reputation: 1721

is it possible to shorten a conditional statement checking for a bunch of different values for a single variable

if (hello == 50 || hello == 60 || hello == 70) {

would it be possible to shorten this to something like that ?

if (hello == (50,60,70));

or something along those line, just to avoid having to constantly rewriting the same variable.

Upvotes: 2

Views: 122

Answers (6)

user unknown
user unknown

Reputation: 36250

Another solution:

if (Arrays.asList (new Integer [] {50, 60, 70}).contains (hello))
    System.out.println ("contains!");

You have to use Intger, not int, in the Array declaration. Much boilerplate code, but when growing, it might be useful.

An initial costy way is a method with an Parameter as Object ellipse:

public static boolean contains (Object sample, Object... os) {
    for (Object o: os) 
        if (o.equals (sample))
            return true;
    return false;
}

which is cheap in usage:

    if (ArrayCheck.contains (hello, 50, 60, 70)) 
        System.out.println ("contains!");

A typesafe method which takes an would better, but again more costly to use - you would have to produce an instance of ArrayCheck for your type first:

public class ArrayCheck <T>
{
    public boolean contains (T sample, T... ts) {
        for (T t: ts) 
            if (t == sample) 
                return true;
        return false;
    }
 // ...
    ArrayCheck <Integer> ac = new ArrayCheck <Integer> ();
    if (ac.contains (hello, 50, 60, 70)) 

but acceptable, if you have multiple invocations of that kind with the same type.

Upvotes: 1

user unknown
user unknown

Reputation: 36250

You don't save much, but if you happen to have more than 3 ints, let's say 10 or 20 ...

if (Arrays.binarySearch (new int [] {50, 60, 70}, hello) >=0)
    System.out.println ("contains!");

Upvotes: 0

emory
emory

Reputation: 10891

How about

if ( 0 == ( ( ( hello / 10 ) - 5 ) / 3 ) ) 

Upvotes: 2

nate_weldon
nate_weldon

Reputation: 2349

there is multiple ways to do it.

you could build in a new method

if(checkValue(hello, 50, 60, 70))
{
   // something
}

private boolean checkValue(data, Integer a, Integer b, Integer c)
{
   return (hello == a || hello == b || hello == c) 
}

or you could build up a collection

but you can't really get round having to do the check somewhere.

and java does not support operator overloading so something like this in C++

if( hello == (50, 60, 70))

would be valid (if you overloaded == ) but not java

Upvotes: 0

Matt Taylor
Matt Taylor

Reputation: 1166

One possible way is with a collection.

Set<Integer> specialNumbers = new HashSet<Integer>();

specialNumbers.add(50);
specialNumbers.add(60);
specialNumbers.add(70);

if(specialNumbers.contains(hello)) { 
//do stuff 
}

Upvotes: 7

Mahesh
Mahesh

Reputation: 34625

Not possible. You can prefer writing switch.

switch(hello)
{
   case 50:
   case 60:
   case 70: // Do some thing
            break;
}

Upvotes: 5

Related Questions