[英]How to input null or EOF in kotlin using readline
I'm doing beginner level Kotlin and I'm trying to do a multi line input and I found this code:我正在做初学者级别的 Kotlin 并且我正在尝试进行多行输入,我发现了以下代码:
var originalTexts: MutableList<String> = mutableListOf()
while (true) {
val originalText = readLine()
if (originalText == null) break
originalTexts.add(originalText)
}
The problem is I dont know how to input null or EOF in the input stream in console using readLine().问题是我不知道如何使用 readLine() 在控制台的输入 stream 中输入 null 或 EOF。 I tried ctrl+z or ctrl+d but is not working.
我尝试了 ctrl+z 或 ctrl+d 但不起作用。 Beginner here please help guys.
初学者在这里请帮助大家。
Edit: I'm using Intellij in Windows OS编辑:我在 Windows 操作系统中使用 Intellij
A null return from readLine()
indicates an end-of-file condition — basically, there can be no more input.从
readLine()
返回的 null 表示文件结束条件——基本上,不能再有输入了。
But whether and how that can happen depends on how you're running the program:但是这是否会发生以及如何发生取决于您如何运行程序:
If you're running it in a terminal window, then you can signal end-of-file with a special key combination, which depends on the platform:如果您在终端 window 中运行它,那么您可以使用特殊的组合键发出文件结束信号,具体取决于平台:
Ctrl
+ D
Ctrl
+ D
Ctrl
+ X
then Return
Ctrl
+ X
然后Return
If you're running it with the input redirected from a file (eg by appending <filename
to the command), then you'll get an end-of-file condition after reading all the content of the file.如果您使用从文件重定向的输入运行它(例如,通过将
<filename
附加到命令),那么您将在读取文件的所有内容后获得文件结束条件。
Some IDEs may provide their own combinations.一些 IDE 可能会提供自己的组合。 For example:
例如:
Some IDEs may provide no way at all.一些 IDE 可能根本没有提供任何方法。
Some IDEs (eg online) don't support user input, and so readLine()
will always return null.某些 IDE(例如在线)不支持用户输入,因此
readLine()
将始终返回 null。
Due to the confusion that this can cause, Kotlin 1.6 added a readln()
function, which never returns null: instead, it throws a RuntimeException
if end-of-file is reached, or on platforms such as Kotlin/JS which don't support user input in this way. Due to the confusion that this can cause, Kotlin 1.6 added a
readln()
function, which never returns null: instead, it throws a RuntimeException
if end-of-file is reached, or on platforms such as Kotlin/JS which don't以这种方式支持用户输入。
(That may make it slightly easier to write beginner programs, but IMHO it's merely turning a compile-time issue into a hidden run-time one, which seems like a backward step for a language as clear and safe as Kotlin…) (这可能会使编写初学者程序稍微容易一些,但恕我直言,它只是将编译时问题变成了隐藏的运行时问题,对于像 Kotlin 这样清晰和安全的语言来说,这似乎是一种倒退……)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.