Ehsan Kia
Ehsan Kia

Reputation: 1544

Clean way of reading all input lines in Kotlin

A common pattern when doing coding challenges is to read many lines of input. Assuming you don't know in advance how many lines, you want to read until EOF (readLine returns null).

Also as a preface, I don't want to rely on java.utils.* since I'm coding in KotlinNative, so no Scanner.

I would like to maybe do something like

val lines = arrayListOf<String>()
for (var line = readLine(); line != null; line = readLine()) {
    lines.add(line)
}

But that clearly isn't valid Kotlin. The cleanest I can come up with is:

while (true) {
    val line = readLine()
    if (line == null) break
    lines.add(line)
}

This works, but it just doesn't seem very idiomatic. Is there a better way to read all lines into an array, without using a while/break loop?

Upvotes: 6

Views: 6229

Answers (3)

StevenHe
StevenHe

Reputation: 446

Most likely, you want to loop through them. As a more complicated example, consider that you want to call reader.readLine() continuously, and you want both the value and the index of the value:

generateSequence(reader::readLine).mapIndexed { index, line ->
    // Do stuff
}

Upvotes: 0

s1m0nw1
s1m0nw1

Reputation: 82027

I guess you're talking about reading from System.in (stdin) here. You could make that work with sequences:

val lines = generateSequence(readLine()) {
    readLine()
}

lines.take(5).forEach { println("read: $it") }

We begin our sequence with a first readLine (the sequence's seed) and then read the next line until null is encountered. The sequence is possibly infinite, therefore we just take the first five inputs in the example. Read about details on Sequence here.

Upvotes: 6

Kiskae
Kiskae

Reputation: 25603

generateSequence has the nice property that it will complete if the internal generator returns null and accepts only a single iteration, so the following code could be valid:

val input = generateSequence(::readLine)
val lines = input.toList()

Then like s1m0nw1's answer you can use any of the available Sequence<String> methods to refine this as desired for your solution.

Upvotes: 14

Related Questions