What is the equivalent of Java Scanner
in Kotlin?
I have used readLine()
but I'd like to know whether it's type safe or not?
You can try something like this
val scan = Scanner(System.`in`)
val n = scan.nextLine().trim().toInt()
Since "in" is a Kotlin keyword
Kotlin reuses many existing Java libraries and it's perfectly fine to do so with Scanner
. Otherwise, readLine
simply uses System.in
as you can observe here, which might be a simple alternative for you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With