1

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!

ShadowWolf
  • 51
  • 8
  • `if (seatNum <= 200) { System.out.println("Row "+ (int)Math.ceil(seatNum/(float)20)); } else if (seatNum <= 216) { System.out.println("Row "+ (int)Math.ceil((seatNum-45)/(float)16)); } else if (seatNum > 216) { System.out.println("Row "+ (int)Math.ceil((seatNum-49)/(float)15)); }` – pokeybit May 16 '17 at 21:57
  • 1
    What about **201**. It doesn't fall into any condition!!! – Shaishav Jogani May 16 '17 at 22:03
  • Seat 201 might be for the fake audience member, if this is a Hypnosis show. Or it may just be unlucky in theatre terms. Don't jinx his code man! – pokeybit May 16 '17 at 22:15
  • I may have forgotten to include 201 in my code – ShadowWolf May 17 '17 at 01:35

10 Answers10

9

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.

Turing85
  • 18,217
  • 7
  • 33
  • 58
  • 4
    The math is not quite right. `setNum == 20` should yield `"Row 1"`, but yours yields `"Row: 2"`. It also yields a different result than the OP's if `setNum` is negative. – John Bollinger May 16 '17 at 21:47
  • Nit: this is equivalent only if seat numbers are all positive. That's likely, but should be noted. Also, integer division in Java does not always round down -- it always rounds towards zero, which is down for positive numerator and denominator. – Andy Thomas May 16 '17 at 21:52
  • There is probably a bug in OP's code because seat 201 seems to be missing, making things unnecessarily complex. – Mick Mnemonic May 16 '17 at 22:08
  • @JohnBollinger should be fixed now. – Turing85 May 16 '17 at 22:11
  • @MickMnemonic This only makes it more interesting ;) – Turing85 May 16 '17 at 22:11
  • 1
    @MickMnemonic turned out... I was thinking too complicated. Code should be (relatively) readable now. – Turing85 May 16 '17 at 22:36
  • Thanks, I did mess up on the 201 in the code, but this is perfect! – ShadowWolf May 17 '17 at 01:38
4
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;
    }
}
DAle
  • 8,990
  • 2
  • 26
  • 45
  • You are missing one corner case (seat `201` is not defined in OPs code), but otherwise this looks like a clean and scalable solution. – Turing85 May 16 '17 at 22:56
3

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");
Ayush Seth
  • 1,169
  • 10
  • 21
2

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);
Turing85
  • 18,217
  • 7
  • 33
  • 58
dumbPotato21
  • 5,669
  • 5
  • 21
  • 34
2

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];
assylias
  • 321,522
  • 82
  • 660
  • 783
1

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
Community
  • 1
  • 1
Yahya
  • 13,349
  • 6
  • 30
  • 42
  • Note that its correct name is the [conditional operator](https://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.25). – Andy Turner May 16 '17 at 22:15
  • I've been told by a teacher to never use those. Would you agree? – qwerty Jan 20 '20 at 18:36
  • @ap It depends on why your teacher doesn't recommend using them! Conditional Operator ? : is very popular, however, your teacher's point might be for readability purposes, since beginners might get confused when they see them! – Yahya Jan 21 '20 at 12:19
1

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";
}
qwerty
  • 810
  • 1
  • 9
  • 26
gogog
  • 410
  • 5
  • 9
0

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));
Shaishav Jogani
  • 2,111
  • 3
  • 23
  • 33
  • 1
    every interval is not 20 – Ayush Seth May 16 '17 at 21:45
  • Still buggy. with this calculation, seat `216` would be in row `12` instead of row `11`. Also, your output does not correspond with OPs code for number `< 0`. And there is the problem with seat `201`... – Turing85 May 16 '17 at 22:52
0
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

pokeybit
  • 1,032
  • 11
  • 18
  • `ceil` has no effect other than casting the `int` expression `seatNum/20` to `double` here. You'd need to make sure that floating point division is used, e.g. `Math.ceil(seatNum / 20.0)`. – Andy Turner May 16 '17 at 22:13
  • Yeah, I knew that wasn't Java, I just put the general jist there :) – pokeybit May 16 '17 at 22:16
0

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).

xinaiz
  • 7,744
  • 6
  • 34
  • 78