1

So lets say a switch statment is,

switch (month) {  
    case 1: monthString = "January";  
        break;  
    case 2: monthString = "February";  
        break;  
    case 3: monthString = "March";  
        break;  
    case 4: monthString = "April";  
        break;  
    case 5: monthString = "May";  
        break;  

Is it possible to shorten that to something like

switch (month) {
    cases 1-3: monthString = "January";
        break;
    case 4-5: monthString = "April";
        break;

So that multiple case numbers are under one case? I'm doing it as I have 100 cases. 30 lead to one answer, 20 to another, 5 to another etc... so if I can use multiple cases I should cut down the bulk of the code by alot. I should also mention at each case I will want to do a few things and if I use a series of if else statements it only lets me perform one action so I cannot seem to go that route. Thanks for any help! sorry I'm new at this!

Dodo
  • 93
  • 2
  • 10

8 Answers8

4

Is it possible to shorten that to something like

It's better explained in Java Tutorial The switch Statement

Yes you can do it.

Some of the key points:

  • Don't forget to add the break statement.
  • Add single break statement after all grouped cases.
  • Never forget to add default case as well.

sample code:

    int month = 1;
    String monthString = null;

    switch (month) {
        case 1:
        case 2:
        case 3:
            monthString = "January";
            break;
        case 4:
        case 5:
            monthString = "April";
            break;
        ...
        default:
            ...
    }
Braj
  • 46,415
  • 5
  • 60
  • 76
  • When doing something like this you should leave a comment in the code that this was intended. – Charles Smartt Aug 15 '14 at 03:34
  • What do you mean? Does intend change the logic of the program? – Braj Aug 15 '14 at 04:30
  • As part of my job I maintain code. There is no documentation, all I can do is read and run it. If you use fall through cases, there are two reasons - it is intentional or it was a mistake. I ran into such a problem. It is just a comment, like //intentional – Charles Smartt Aug 15 '14 at 12:46
2

You might consider an array.

static String[] MONTHS = ",January,January,January,April,April".split(",");

monthString = MONTHS[month];
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
1

I'm doing it as I have 100 cases. 30 lead to one answer, 20 to another, 5 to another etc

based on that why not compare size of the int instead of using a lot of fall-through cases. No matter what you do 100 cases, will still be a lot of tedious code to write but i can't imagine any one doing 30 fall through cases or 20 fall through case. Since your comparing int not String you can go with if statements like so:

 if (month > 0 && month <= 30) {
    monthString = "January";
 }

 else if (month > 30 && month <= 50) {
    monthString = "February";
 }

 else if (month > 50 && month <= 55) {
    monthString = "March";
 }

 // 55 cases have been matched

compared to:

switch (month) {
    case 1:
    case 2:
    case 3:
    ...
    case 28:
    case 29:
    case 30:
        monthString = "January";
        break;
}
Jay Harris
  • 4,201
  • 17
  • 21
0

If I understand your question then yes. But it's not

switch (month) {
cases 1-3: monthString = "January";
    break;
case 4-5: monthString = "April";
    break;
}

It would be

switch (month) {
cases 1: case 2: case 3: monthString = "January";
    break;
case 4: case 5: monthString = "April";
    break;
}

Case statements fall through without a break. Also, you may want a default: it's like an else if your number might be outside any explicit case.

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
0

yes it is possible

switch (month) {  
    case 1: 

    case 2:   

    case 3:  monthString = "January";   
        break;  
    case 4: 

    case 5:  
        monthString = "April";    
SpringLearner
  • 13,738
  • 20
  • 78
  • 116
0

Just use an if-else. It will be easier for this scenario.

Charles Smartt
  • 349
  • 2
  • 8
0

You could use a hashmap:

HashMap mySet = new HashMap()

and then fill it with the appropriate sets:

(new Integer(1), "January")

(new Integer(2), "January")

(new Integer(3), "January")

(new Integer(4), "April")

(new Integer(5), "April")

then your code looks like:

monthString = null;
mySet.containsKey(new Integer(month)) && monthString = mySet.getValue(new Integer(month))

That way you're taking advantage of short circuiting and having

"monthString = mySet.getValue(new Integer(month))" 

only execute if that key is in the set. You can also make a function which populates your HashMap, thereby making it easy to maintain.

You can read up more on short-circuiting in java here: Java logical operator short-circuiting

Community
  • 1
  • 1
Devarsh Desai
  • 5,984
  • 3
  • 19
  • 21
0
switch (month) {

  case 1:
  case 2:
  case 3:
  monthString = "January";
    break;
  case 4:
  case 5:
  monthString = "April";
    break;
}

You can use this and split function given by @Peter works great.

Deepanshu J bedi
  • 1,530
  • 1
  • 11
  • 23