ShadowWolf
ShadowWolf

Reputation: 51

Is it possible to shorten this if-else-if statement, and how do I do so?

I have created this if-else-if statement and am wondering if I am able to shorten it so that it does not take as much space. If anyone knows how to do this, please help!

if (seatNum <= 20) System.out.println("Row 1");
else if (seatNum > 20 && seatNum <= 40) System.out.println("Row 2");
else if (seatNum > 40 && seatNum <= 60) System.out.println("Row 3");
else if (seatNum > 60 && seatNum <= 80) System.out.println("Row 4");
else if (seatNum > 80 && seatNum <= 100) System.out.println("Row 5");
else if (seatNum > 100 && seatNum <= 120) System.out.println("Row 6");
else if (seatNum > 120 && seatNum <= 140) System.out.println("Row 7");
else if (seatNum > 140 && seatNum <= 160) System.out.println("Row 8");
else if (seatNum > 160 && seatNum <= 180) System.out.println("Row 9");
else if (seatNum > 180 && seatNum <= 201) System.out.println("Row 10");
else if (seatNum > 201 && seatNum <= 216) System.out.println("Row 11");
else if (seatNum > 216 && seatNum <= 231) System.out.println("Row 12");
else if (seatNum > 231 && seatNum <= 246) System.out.println("Row 13");
else if (seatNum > 246 && seatNum <= 261) System.out.println("Row 14");
else if (seatNum > 261 && seatNum <= 276) System.out.println("Row 15");
else if (seatNum > 276 && seatNum <= 291) System.out.println("Row 16");
else if (seatNum > 291 && seatNum <= 306) System.out.println("Row 17");
else if (seatNum > 306 && seatNum <= 321) System.out.println("Row 18");
else if (seatNum > 321 && seatNum <= 336) System.out.println("Row 19");
else if (seatNum > 336 && seatNum <= 351) System.out.println("Row 20");

Edit: Included seat 201. Sorry for all the confusion! 🤒

Upvotes: 1

Views: 487

Answers (10)

Turing85
Turing85

Reputation: 20185

We can use the fact that int-divisions in Java will always round towards zero, meaning 5 / 2 == 2 and -5 /2 == -2.

For seatNum <= 0, the row is always 1. As long as seatNum <= 200, the row is ((seatNum - 1) / 20) + 1. If it is > 201, we can simply take ((seatNum - 202) / 15) + 11. We substract 202 because the numbers are set off by 2: 216 is still in row 11, 217 is in row 12. Same holds for 231 and 232. With this calculation, the undefined seat 201 will be places in row 11, but ignoring this input or throwing an Exception instead is no big deal.

// if (seatNum != 201) { // optional, to neglect seat 201
    System.out.print("Row: "); // yes, I am that lazy...
    if (seatNum <= 0) {
        System.out.println(1);
    } else if ((seatNum <= 200) /* && (seatNum > 0) */) {
        System.out.println(((seatNum - 1)/ 20) + 1);
    } else /* if (seatNum > 200) */ {
        System.out.println(((seatNum - 202) / 15) + 11);   
    }
// }

If the rows get even more complicated / irregular, I would probably deploy a slight variation of DAle's solution. This seems pretty readable and scalable.

Upvotes: 9

gogog
gogog

Reputation: 420

Let's make a method for your code. Since the return keyword terminates the method, it can be used to shorten the if-else tree as follows:

public static void main(String[] args) {
    ArrayList<Integer>index=new ArrayList<Integer>();
    ArrayList<String>excep=new ArrayList<String>();
    for (int i = 0; i < 400; i++) {
        if(!seat(i).equals(seat(i-1))){
            excep.add(seat(i-1));
            index.add(i);
        }
    }

    for (int test = 0; test < 400; test++) {
        if(test<352){
            int i=0;
            for(;i<index.size();i++){
                if(index.get(i)-test>=0){
                    if(index.get(i)-test==0)
                        i++;
                    break;
                }   
            }
            System.out.println(excep.get(i));;  
        }else if(test==201){
            System.out.println(seat(test));
        }else
            System.out.println("null");
    }       

}

public static String seat(int seatNum) {
    if (seatNum <= 20)
        return ("Row 1");
    else if (seatNum <= 40)
        return ("Row 2");
    else if (seatNum <= 60)
        return ("Row 3");
    else if (seatNum <= 80)
        return ("Row 4");
    else if (seatNum <= 100)
        return ("Row 5");
    else if (seatNum <= 120)
        return ("Row 6");
    else if (seatNum <= 140)
        return ("Row 7");
    else if (seatNum <= 160)
        return ("Row 8");
    else if (seatNum <= 180)
        return ("Row 9");
    else if (seatNum <= 200)
        return ("Row 10");
    else if (seatNum <= 216)
        return ("Row 11");
    else if (seatNum <= 231)
        return ("Row 12");
    else if (seatNum <= 246)
        return ("Row 13");
    else if (seatNum <= 261)
        return ("Row 14");
    else if (seatNum <= 276)
        return ("Row 15");
    else if (seatNum <= 291)
        return ("Row 16");
    else if (seatNum <= 306)
        return ("Row 17");
    else if (seatNum <= 321)
        return ("Row 18");
    else if (seatNum <= 336)
        return ("Row 19");
    else if (seatNum <= 351)
        return ("Row 20");
    return "null";
}

Upvotes: 1

Yahya
Yahya

Reputation: 14062

I would like to mention the Conditional Operator ? : . Although it does NOT give that much save of space, but it's good to keep it in mind as some programmers consider it more readable and a little bit faster in executing.

String row = (seatNum <= 20)  ? "Row 1"  : (seatNum <= 40)  ? "Row 2"  :
             (seatNum <= 60)  ? "Row 3"  : (seatNum <= 80)  ? "Row 4"  :
             (seatNum <= 100) ? "Row 5"  : (seatNum <= 120) ? "Row 6"  :
             (seatNum <= 140) ? "Row 7"  : (seatNum <= 160) ? "Row 8"  :
             (seatNum <= 180) ? "Row 9"  : (seatNum <= 200) ? "Row 10" :
             (seatNum <= 216) ? "Row 11" : (seatNum <= 231) ? "Row 12" :
             (seatNum <= 246) ? "Row 13" : (seatNum <= 261) ? "Row 14" :
             (seatNum <= 276) ? "Row 15" : (seatNum <= 291) ? "Row 16" :
             (seatNum <= 306) ? "Row 17" : (seatNum <= 321) ? "Row 18" :
             (seatNum <= 336) ? "Row 19" : (seatNum <= 351) ? "Row 20" : null;

System.out.println(row); // to see the result

Upvotes: 1

xinaiz
xinaiz

Reputation: 7788

You could simply write:

System.out.println("Row " + ((seatNum <= 200)?
                            ((Math.max(20, seatNum) - 1) / 20) + 1 
                          : ((seatNum - 202) / 15) + 11));

I ignored the 201 case which is probably an error (Change 202 to 201 if necessary).

Upvotes: 0

dumbPotato21
dumbPotato21

Reputation: 5695

Yes, sure

String s = "";
System.out.print("Row No. ");

if      (seatNum <=  20) s = "1";
else if (seatNum <=  40) s = "2";
else if (seatNum <=  60) s = "3";
else if (seatNum <=  80) s = "4";
else if (seatNum <= 100) s = "5";
else if (seatNum <= 120) s = "6";
else if (seatNum <= 140) s = "7";
else if (seatNum <= 160) s = "8";
else if (seatNum <= 180) s = "9";
else if (seatNum <= 200) s = "10";
else if (seatNum <= 216) s = "11";
else if (seatNum <= 231) s = "12";
else if (seatNum <= 246) s = "13";
else if (seatNum <= 261) s = "14";
else if (seatNum <= 276) s = "15";
else if (seatNum <= 291) s = "16";
else if (seatNum <= 306) s = "17";
else if (seatNum <= 321) s = "18";
else if (seatNum <= 336) s = "19";
else if (seatNum <= 351) s = "20";

System.out.println(s);

Upvotes: 2

Shaishav Jogani
Shaishav Jogani

Reputation: 2121

What about this...

if(seatNum <= 0 ) 
    System.out.println("Row 1");
if(seatNum <= 200)
    System.out.print("Row "+ (int)Math.ceil(seatNum /20.00));
else
    System.out.print("Row "+ (int)Math.ceil((seatNum-201) /15.00 + 10));

Upvotes: 0

assylias
assylias

Reputation: 328608

Two options worth exploring are using a TreeMap and binary search.

With a TreeMap, you could do something like this:

private static final TreeMap<Integer, Integer> map = new TreeMap<>();
map.put(20, 1);
map.put(40, 2);
//etc

Then in your method (add null checks as necessary):

int row = map.ceilingEntry(seatNum).getValue();

The binary search option would look like this:

private static final int[] rowEnds = { 20, 40, ... };

(that array needs to be sorted) and in your method:

int rowIndex = Arrays.binarySearch(rowEnds, seatNum);
if (rowIndex < 0) rowIndex = - rowIndex + 1;
int row = rowEnds[rowIndex];

Upvotes: 2

DAle
DAle

Reputation: 9117

int highestRowSeat[] = {20, 40, 80, 100, 120, 140, 160, 180, 200, 216, 231, 246, 261, 276, 306, 321, 336, 351};

for (int row = 1; row <= highestRowSeat.length; ++row) {
    if (seatNum <= highestRowSeat[row-1]) {
        System.out.println("Row " + row);
        break;
    }
}

Upvotes: 4

Ayush Seth
Ayush Seth

Reputation: 1209

One thing you can reduce is the number of checks, in this case, since you are using if...else the following conditions will be checked only if the previous one is false, so you can remove the greater than check from every else if because it will be auto checked by the previous one.

EDIT : Added additional check for the missing 201

if (seatNum <= 20) System.out.println("Row 1");
else if (seatNum <= 40) System.out.println("Row 2");
else if (seatNum <= 60) System.out.println("Row 3");
else if (seatNum <= 80) System.out.println("Row 4");
else if (seatNum <= 100) System.out.println("Row 5");
else if (seatNum <= 120) System.out.println("Row 6");
else if (seatNum <= 140) System.out.println("Row 7");
else if (seatNum <= 160) System.out.println("Row 8");
else if (seatNum <= 180) System.out.println("Row 9");
else if (seatNum <= 200) System.out.println("Row 10");
else if (seatNum <= 216 && seatNum != 201) System.out.println("Row 11");
else if (seatNum <= 231) System.out.println("Row 12");
else if (seatNum <= 246) System.out.println("Row 13");
else if (seatNum <= 261) System.out.println("Row 14");
else if (seatNum <= 276) System.out.println("Row 15");
else if (seatNum <= 291) System.out.println("Row 16");
else if (seatNum <= 306) System.out.println("Row 17");
else if (seatNum <= 321) System.out.println("Row 18");
else if (seatNum <= 336) System.out.println("Row 19");
else if (seatNum <= 351) System.out.println("Row 20");

Upvotes: 3

pokeybit
pokeybit

Reputation: 1032

if (seatNum <= 200) {
    System.out.println("Row "+ ceil(seatNum/20));
}
else if (seatNum <= 216) {
    System.out.println("Row "+ ceil((seatNum-45)/16));
}
else if (seatNum > 216) {
    System.out.println("Row "+ ceil((seatNum-49)/15));
}

I don't know the correct function names or anything about Java, but the logic of this works

Upvotes: 0

Related Questions