Switch to readln

readln throws an exception when a file hits EOF, wheras readLine returns null. That puts us in an infinite loop if we hit EOF.
This commit is contained in:
John Long
2022-01-05 23:22:46 -08:00
parent 916fa6f252
commit 71c7541905

View File

@@ -37,14 +37,14 @@ fun main() {
// an answer or a blank string // an answer or a blank string
fun ask(question: String): String { fun ask(question: String): String {
print("$question? ") print("$question? ")
return readLine()?.uppercase() ?: "" return readln().uppercase() ?: ""
} }
// Special case for a "yes or no" question, returns true of yes // Special case for a "yes or no" question, returns true of yes
fun askYesOrNo(question: String): Boolean { fun askYesOrNo(question: String): Boolean {
return generateSequence { return generateSequence {
print("$question? ") print("$question? ")
readLine() readln()
}.firstNotNullOf { yesOrNo(it) } }.firstNotNullOf { yesOrNo(it) }
} }