0

I'm doing beginner level Kotlin and I'm trying to do a multi line input and I found this code:

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(). I tried ctrl+z or ctrl+d but is not working. Beginner here please help guys.

Edit: I'm using Intellij in Windows OS

bernie
  • 1
  • 1
  • What platform are you using? Are you running this in a terminal window on macOS, or Linux, or Windows? Or from an IDE? Or on a web site? Or in some other way? – gidds Jul 30 '22 at 10:20
  • Thank you for checking out my question gidds! I'm using Intellij on Windows OS. – bernie Jul 30 '22 at 10:27
  • I'm also using git bash as my teminal in Intellij @gidds – bernie Jul 30 '22 at 10:56
  • try `originalText == ""`, then in console when you hit enter and pass an empty string it will break – Dewerro Jul 30 '22 at 10:58
  • Btw you can't input null, because it always will be string, just empty – Dewerro Jul 30 '22 at 11:10
  • @dewerro Thank you for answering my question! Your solution is the same way with val isLineBlank = originalText.isNullOrEmpty() if (isLineBlank == true) break Right? But I need to input(or copy paste in the input stream) multiple paragraphs separated by blank lines. That is why I'm looking for the EOF or NULL keyboard command. So if the input read for example in c programming ctrl-z, it will terminate the looping for the readline if the condition is like this: (originalText==EOF). – bernie Jul 30 '22 at 11:45

1 Answers1

0

A null return from readLine() indicates an end-of-file condition — basically, there can be no more input.

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:

    • On macOS and Linux: Ctrl+D
    • On Windows: Ctrl+X then Return
  • If you're running it with the input redirected from a file (e.g. by appending <filename to the command), then you'll get an end-of-file condition after reading all the content of the file.

  • Some IDEs may provide their own combinations. For example:

    • IntelliJ on macOS: Cmd+D. (Ctrl+D starts the debugger instead.)
    • IntelliJ on Windows: Ctrl+D.
  • Some IDEs may provide no way at all.

  • Some IDEs (e.g. online) don't support user input, and so readLine() will always return 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.

(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…)

gidds
  • 16,558
  • 2
  • 19
  • 26
  • Thanks for the answer gidds! Btw I'm using git Bash as my terminal in Intellij, when I try to do **Ctrl + D** the whole program terminates! I only want to terminate the looping in readline. Btw my goal is to input mulitple paragraphs(paragraphs separated by new line) and compare it to another multiple paragraphs so I need newline/Empty too. For the mean time I created a unique string to feed to the readline in order for it to stop looping. **if(originalText=="000EOF") break** – bernie Jul 30 '22 at 13:29
  • @bernie Different versions of IntelliJ have had various problems with entering EOF. (It wasn't implemented at all in early versions, and later on it seems to have got broken at least once.) — But if you want to process multiple files, why not enter the file_names_ (on the command line, or entered at the terminal), and then the programme can read the lines from each file in turn? Kotlin has some nice extension methods such as `useLines()` to make that sort of thing easy. – gidds Jul 30 '22 at 14:08