Home > Software engineering >  Kotlin readln() does not work as intended
Kotlin readln() does not work as intended

Time:05-25

If I assign values from readln() successively, they do not work properly; the second variable is an empty line.

fun main() {
  val first = readln()
  val second = readln()
  println(first)
  println(second) 
}

Does the second variable reads in some empty string or changeLine thing? If I switch the order of line 3 and line 4, everything just works fine.

fun main() {
  val first = readln()
  println(first)
  val second = readln()
  println(second)
}

Just wondering what happened in the first code?

edit : I tried this in Intellij community under Win11 and Ubuntu in VMware. Here is the picture: console debug I ran the code by left-clicking the green triangle.

edit: the first code worked fine on my old laptop, but after I updated my Intellij to the latest version, the first code broke as well.

CodePudding user response:

In 2022.1.1 there is some bug in the console causing readLine (among other readers) to skip user input. [1] This bug is fixed in 2022.1.2 Preview. [2] Oddly, the problem seems to persist in 2022.2 EAP.

[1] https://youtrack.jetbrains.com/issue/IDEA-293951

[2] https://youtrack.jetbrains.com/articles/IDEA-A-162/IntelliJ-IDEA-2022-1-2-Preview-221-5787-3-build-Release-Notes

  • Related