switch / case request with boolean

If you really want a "sexy-java-way" (but that depends what you understand as such) you can do something like (Java 7 required):

boolean user, pass;

switch (user + "-" + pass) {
    case "false-false":
        ...
    case "false-true":
        ...
    case "true-false":
        ...
    case "true-true":
        ...
    default:
        throw new RuntimeException(
            "something strange happening here, user: " + user + ",pass: " + pass);
}

but I would prefer to do just 2 distinct checks each with his owns message, the message being joined for presentation. (and not sure if that could be considered "sexy-java-way", more like a 'workaround')


You can't switch over boolean[], only over integral types. To convert the booleans to an int, you could use a bit mask for the 2 booleans, like for example this:

int val = 0;
if (user) val |= 0x1;
if (pass) val |= 0x2;

switch (val) {
case 0: // Both too short
case 1: // User Ok, pass too short
case 2: // User too short, pass ok
case 3: // Both Ok
}