My code:
fun main() {
println("What's your name?")
val name= readLine()
println("Hello $name")
}
Output:
What's your name?
Abhas
Hello
My question is: Why $name is not getting printed? After taking input ' Abhas ' it doesn't show anything and then after I pressed Enter and it prints ' Hello ' only.
I'm not sure why it's not working for you - it is reading some input or it wouldn't get to the
printlnstatement, and it is printing the value ofname(which has a value, otherwise it would say null).At a guess, it's something to do with the way you're providing input, where the first line it reads is empty, so you get an empty string (""). You said your output is like this:
Are you hitting enter/return to get to the next line before you type Abhas? That does nothing in the REPL I'm using, but it might count as an (empty) input line in your console