Home > Mobile >  Kotlin: 'if' must have both main and 'else' branches if used as an expression
Kotlin: 'if' must have both main and 'else' branches if used as an expression

Time:07-21

I am trying to run this code:

fun main() {
    print("This is a calculator")
    print("enter your first number")
    val no1 = readLine()!!
    print("enter your operation")
    val operation1 = readLine()!!
    print("enter your second number")
    val no2 = readLine()!!

    val result = if(operation1 == '*')
    print("$val no1 * $val no2")
}

But I get the following error:

'if' must have both main and 'else' branches if used as an expression Operator '==' cannot be applied to 'String' and 'Char' Keyword cannot be used as a reference Keyword cannot be used as a reference

It also keeps coming up with Kotlin: Keyword cannot be used as a reference within my IDE.

I am very new to Kotlin and I am just trying to create a basic calculator, can you please help?

CodePudding user response:

That code has three different compilation errors, and many other things to fix:

  • In the line:
    print("$val no1 * $val no2")
    the $val is a string template which would fill in the value of the variable val — but val isn't a variable; it's a keyword. (That's what's causing the ‘Keyword cannot be used as a reference’ error.) I think you want $no1 and $no2.
  • The test if(operation1 == '*') is comparing a string with a character. (Single-quotes give a character constant. A string contains characters, but it is not a character, so it can never be equal to a character.) The compiler tells you this with the error ‘Operator '==' cannot be applied to 'String' and 'Char'’. The simplest way to fix it is to provide a string by using double-quotes instead: "*".
  • The pair of lines:
    val result = if(operation1 == '*')
    print("$val no1 * $val no2")
    is incomplete and doesn't make sense as it is. What do you want to do if operation1 isn't "*"? And what do you want to set result to in each case?
    (It looks like you're trying to use if as an expression, and assign its result to result — but to use if as an expression you need to add an else branch too, so it knows what value to use in all cases. That's causing the compilation error ‘'if' must have both main and 'else' branches if used as an expression’.)
  • If this is the start of some code which will check for *, , and other operators, then maybe you want to set the result and then print it, e.g.:
    val result = if (operation1 == "*") /* … */
    else if (operation2 == " ") /* … */
    else /* … */
    print(result)
    (Though that would be simpler with when instead of if.)
  • If you fix the first issue by changing it to "$no1 * $no2", that will print e.g. 1 * 2. I'm guessing you want instead to calculate the product of two numbers. But no1 and no2 aren't numbers; they're strings. So you first have to convert them into numbers, e.g. with no1.toInt(). You also have to tell the compiler that you want to evaluate the * rather than just printing it. If you just want to print out the number, you could skip the string template entirely:
    print(no1.toInt() * no2.toInt())
    However, if you want to use a string template (e.g. if you want to add some text), you'd have to use the ${…} format to mark out the bit you want to evaluate:
    print("${no1.toInt() * no2.toInt()}")
  • Some or all of the print(…) calls should probably be println(…). (println() adds a line-break after the string, while print() doesn't, leaving the next input or output to follow immediately on the same line.)
  • Even if all that's fixed, the code could fail in several ways if the user doesn't enter the expected input. (Both !! and toInt() can throw exceptions, e.g. if you press Enter without typing a number.) That's fine for an exercise, of course; but if this code ever got to production, you'd want to handle those possibilities better.

CodePudding user response:

When you use $val, it is trying to access the val variable, but as $val is undefined, it can't, and freaks out because it doesn't know what to do.

Instead of print("$val no1 * $val no2"), you can use:

print("$no1 * $no2")

The code above will use the $no1 and $no2 variables that you want it to use.

CodePudding user response:

In line 4 and 8, the input should be converted to Int, like this:

val no1 = readLine()!!.toInt()
val no2 = readLine()!!.toInt()

In the last line, val should not be written and should be as follows:

print("$no1 * $no2")

You can also add other operators using else if in the continuation of the if command

  • Related