Warm tip: This article is reproduced from serverfault.com, please click

Odd behavior with kotlin's readLine function

发布于 2020-12-03 03:34:06

I'm picking up kotlin for a new job and I'm working through the intellij hello world app for kotlin.

In the walkthrough it has you create a console app that reads in standard input and prints a greeting:

fun main(args: Array<String>) {
    println("What's your name?")
    val name = readLine()
    println("Hello, $name")
}

Super simple.

The problem I'm running into is that when running the app, I get the prompt too enter my name, I type it in, hit enter, and nothing happens. The app is still running, but it doesn't accept the standard input off of the first enter button press.

If I press enter one or two more times it finally accepts the input but pulls in an empty string (makes sense if it registered each of the two to three enter button presses as a new line each).

The interaction can't be captured in screen shots that I can post here, but I did record a quick video of what's happening: https://vimeo.com/486636891

I looked up the kotlin docs on readLine and they don't really clue me in to what could be happening.

Any idea of what's going wrong here and how to fix it? Ultimately I'm not going to be writing many console apps with kotlin so it's not critical, but the idea of not being able to get through the very first hello-world app in a language/tool nags at me.

UPDATE

Hmm, it must be something with the running of the source code before building the jar.

If I continue through the tutorial to the point where I build the jar and run it the file runs fine:

running as a jar

Still pretty frustrating that it doesn't work without building.

Questioner
Chris Schmitz
Viewed
0
Chris Schmitz 2020-12-07 23:20:53

So after digging for a bit it seemed like it may be an IDE issue, so I submitted it as a bug to JetBrains.

This morning I got an email that the submission was duplicated, marked as a bug, and assigned to someone, so it looks like the issue is def IDE based and hopefully will have a fix in the near future.

bug assigned

Here's the link to the issue if anyone wants to follow the progress or see the future results.