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")
}
It keeps coming up with Kotlin: Keyword cannot be used as a reference. I am very new to kotlyn and I am just trying to create a basic calculator. 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 variableval
— butval
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 ifoperation1
isn't "*"? And what do you want to setresult
to in each case?
(It looks like you're trying to useif
as an expression, and assign its result toresult
— but to useif
as an expression you need to add anelse
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
*
,
val result = if (operation1 == "*") /* … */
else if (operation2 == " ") /* … */
else /* … */
print(result)
(Though that would be simpler withwhen
instead ofif
.) - 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. Butno1
andno2
aren't numbers; they're strings. So you first have to convert them into numbers, e.g. withno1.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 beprintln(…)
. (println()
adds a line-break after the string, whileprint()
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
!!
andtoInt()
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