Switch case Kotlin

Asked

Viewed 8,606 times

8

In Java I use the switch in various situations, as in the example below:

public class SwitchDemo {
    public static void main(String[] args) {

        int month = 8;
        String monthString;
        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;
            case 6:  monthString = "June";
                     break;
            case 7:  monthString = "July";
                     break;
            case 8:  monthString = "August";
                     break;
            case 9:  monthString = "September";
                     break;
            case 10: monthString = "October";
                     break;
            case 11: monthString = "November";
                     break;
            case 12: monthString = "December";
                     break;
            default: monthString = "Invalid month";
                     break;
        }
        System.out.println(monthString);
    }
}

What would that be like in Kotlin, what is the equivalent statement?

3 answers

11


Kotlin works with a different flow control called when.

Your code, using the when, be so.

Obviously the code could be different, but I understand your doubt is only about the use of switch.

fun main(args: Array<String>) {
    val month = 8

    val monthString = when(month) {
        1 -> "Janeiro"
        2 -> "February"
        3 -> "March"
        4 -> "April"
        5 -> "May"
        6 -> "June"
        7 -> "July"
        8 -> "August"
        9 -> "September"
        10 -> "October"
        11 -> "November"
        12 -> "December"
        else -> "Invalid month"      
    }

    println(monthString);
}

See working here.

  • no longer need to use break in this case?

  • Yeah, you don’t need the break.

  • @Guava the default java is the else in this scenario

6

val monthString = when (month) {
    1 -> "January"
    2 -> "February"
    3 -> "March"
    4 -> "April"
    5 -> "May"
    6 -> "June"
    7 -> "July"
    8 -> "August"
    9 -> "September"
    10 -> "October"
    11 -> "November"
    12 -> "December"
    else -> "Invalid month"
}

Actually even in Java I would do differently:

val monthString = if (month < 1 || month > 12) "Invalid month" else arrayOf("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December")[month - 1]

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

0

You can also be worth maps to do this. This approach is very common in switch-less languages like python.

val months = mapOf<Int, String>(
    1 to "January",
    2 to "February"
    3 to "March",
    4 to "April",
    5 to "May",
    6 to "June",
    7 to "July",
    8 to "August",
    9 to "September",
    10 to "October",
    11 to "November",
    12 to "December"
)
val month: String = months.get(1) ?: throw IllegalArgumentException("Invalid month")

It all depends on what makes sense to you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.