Treat console input as untrusted text
Console input arrives as strings. Reading, parsing, and validating are separate operations: a string can be a valid integer but still be outside the range your application accepts.
fun main() {
print("Minutes available: ")
val minutes = readlnOrNull()?.trim()?.toIntOrNull()
if (minutes == null || minutes <= 0) {
println("Enter a positive whole number")
return
}
println("Plan ${minutes / 25} complete study blocks")
}
readlnOrNull() handles end-of-input without an exception. toIntOrNull() handles malformed numbers and values outside the Int range. The branch then checks the application's positive-number rule. Input 60 produces two complete blocks; the remaining ten minutes are not included.
These APIs belong in console programs. An Android app receives input from UI state and must not block its main thread waiting on standard input.
Exercise
Extend the program to report remaining minutes. Test 60, 0, -5, whitespace, hello, and an integer too large for Int.
Check: invalid input must produce a clear message without a stack trace; 60 should produce two blocks and ten remaining minutes.