Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin: Variable 'result' must be initialized

Tags:

kotlin

The compiler is showing error Kotlin: Variable result must be initialized.

Here is the code.

fun main(args: Array<String>) {
    print("Enter two numbers: ")

    // nextDouble() reads the next double from the keyboard
    var first= readLine()!!.toDouble()
    var second = readLine()!!.toInt()

    print("Enter an choice(1-4)): ")
    val operator = readLine()!!.toInt()

    var result: Double

    when (operator) {
        1 -> result = first + second
        2 -> result = first - second
        3 -> result = first * second
        4 -> result = first / second

        else -> {
            println("Error.")
        }
    }

    println("The result is :- " +result)
}
like image 886
Vishal Avatar asked Mar 09 '18 17:03

Vishal


People also ask

Do you have to initialize variables in Kotlin?

Kotlin does not require you to mention the type of a variable when declaring it (thanks to type inference). A variable val must be initialized in the same block of code in which it was declared.

How do you just declare a variable in Kotlin?

Variable declaration. Kotlin uses two different keywords to declare variables: val and var . Use val for a variable whose value never changes. You can't reassign a value to a variable that was declared using val .

How do I initialize a String in Kotlin?

To initialize a String variable in Kotlin, we may assign the String literal to the variable without specifying explicit type, or we may declare a variable of type String and assign a String literal later. The following is a sample code snippet to initialize a variable with a String literal.

How do you declare an empty variable in Kotlin?

To create an empty String in Kotlin, assign the variable with empty double quotes "" , or with String class constructor String() .


1 Answers

The problem is that when you read the value of result here:

println("The result is :- " +result)

result might not be initialized, because here:

var result: Double

when (operator) {
    1 -> result = first + second
    2 -> result = first - second
    3 -> result = first * second
    4 -> result = first / second

    else -> {
        println("Error.")
    }

You are NOT assigning a value to result in the else branch. You have several options, one could be to make result nullable. For instance:

var result = when (operator) {
    1 -> first + second
    2 -> first - second
    3 -> first * second
    4 -> first / second
    else -> null
}

if (result != null) {
    println("The result is :- " + result)
} else {
    println("Error.")
}

Notice that if the operator is not (1-4) the value of result will be null. Also, in your code you're printing "Error" and then again you're attempting to print the result.

To add something more, you could make the thing a bit nicer if you defined your operator with the when statement as a method reference:

print("Enter an choice(1-4)): ")
val operatorCode = readLine()!!.toInt()

val operator: ((Int) -> Double)? = when (operatorCode) {
    1 -> first::plus
    2 -> first::minus
    3 -> first::times
    4 -> first::div
    else -> null
}

if (operator != null) {
    val result = operator.invoke(second)
    println("The result is :- " + result)
} else {
    println("Error.")
}
like image 80
lelloman Avatar answered Oct 27 '22 13:10

lelloman