0

For a switch case imagine I have 5 different cases and three of these share a common action that I don't want to repeat. Following code illustrates perfectly what I want to achieve (but in a switch):

    int x = 5;
    if(x == 1) {
        System.out.println("one");
    } else if (x == 2) {
        System.out.println("two");
    } else {
        String common = "thirty-";
        if (x == 3) {
            method_one(common);
        } else if (x == 4) {
            method_two(common);
        } else if (x == 5) {
            method_three(common);
        }
    }

Can I write this as a switch case elegantly? My current solution is seen below:

    int[] smallGroup = {1,2};
    if (!Arrays.asList(smallGroup).contains(x))
            common = "thiry-";
user2651804
  • 1,464
  • 4
  • 22
  • 45
  • You don't really need a switch at all. Just have an array containing the pre-built values, and pick the one for your `x`. – Andy Turner Jul 02 '17 at 19:00
  • @SJuan76 this question is not asked nor answered in the linked thread. My cases 3 through 5 are completely different. – user2651804 Jul 02 '17 at 19:04
  • @AndyTurner I see that my question may have been poorly formulated. The logic in cases 3 through 5 are supposed to be entirely different. No similarities (except the 'common' variable) such as it appears from the example. – user2651804 Jul 02 '17 at 19:06
  • All the grouping that you can do with `switch` is what is explained in the referenced question. Anyway, in the hopes that you can edit the question to something more answerable... – SJuan76 Jul 02 '17 at 19:11
  • @SJuan76 I wouldn't know what more to edit without hints as to what is being misunderstood in my question. – user2651804 Jul 02 '17 at 19:15

1 Answers1

0

For a switch case imagine I have 5 different cases and three of these share a common action that I don't want to repeat.

The usual way to group cases in switch-case blocks is to simply group up their case tags together. There is no rule that enforces that every case tag must be immediately followed by a block of code.

This is illustrated in the block of code below that converts a fractional day number to hours:minutes:seconds:milliseconds. The code for the MINUTES and SECONDS case is shared, and the cases for them are grouped together:

// HOURS, MINUTES, SECONDS, AND MILLIS are integer constants; f is a fractional day

    for (int i = HOURS; i <= MILLIS; i++) {
        switch (i) {
            case HOURS:
                f = f * 24.0;
                break;
            case MINUTES:  case SECONDS:
                f = f * 60.0;
                break;
            case MILLIS:
                f = f * 1000.0;
                break;
        }
        x = trunc(f);         // trunc() here is equivalent to Math.floor()
        hmsm[i] = (int) x;    // hmsm[] is the result array
        f = f - x;
    }

As an aside, there is a more elegant way to convert a fractional day to H:M:S:M with integer arithmetic, but the above is illustrative.

scottb
  • 9,908
  • 3
  • 40
  • 56
  • Yes this would correspond to if(x == 1 || x == 2). But I have 5 entirely separate cases. – user2651804 Jul 02 '17 at 19:34
  • The words "entirely separate" imply that a switch-case block probably isn't going to be useful, then. Your requirements aren't clear. At a minimum, the cases have to be related by some sort of property or index in common. – scottb Jul 02 '17 at 19:41
  • The "shared" cases should be on separate lines for readability, though. – Lew Bloch Jul 02 '17 at 23:24