basic-computer-games icon indicating copy to clipboard operation
basic-computer-games copied to clipboard

Kotlin Animal goes into an infinite loop on EOF

Open pcholt opened this issue 4 years ago • 4 comments

Animal.kt goes into an infinite loop, on end-of-file. readLine returns null when end-of-file is reached, so looping back to try readline again will not work, it will not prompt again after an end-of-file. Best to throw an exception on interactive readLine sessions returning null:

return readLine()?.uppercase() ?: throw EndOfInputException()

pcholt avatar Jan 05 '22 01:01 pcholt

can we notify the person who authored that PR?

coding-horror avatar Jan 05 '22 03:01 coding-horror

@patimen Can you have a look at this?

pcholt avatar Jan 05 '22 06:01 pcholt

Ah, OK. I think I'll switch to readln(), which will throw an exception instead of return null. I use it in the other Kotlin code for that reason, I must have missed this one.

https://github.com/coding-horror/basic-computer-games/pull/432

patimen avatar Jan 06 '22 07:01 patimen

Thanks @pcholt for the bug report and thanks @patimen for the fix!

journich avatar Jan 12 '22 02:01 journich