Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the equivalent of Java Scanner in Kotlin?

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?

like image 580
esu Avatar asked Feb 27 '18 10:02

esu


2 Answers

You can try something like this

val scan = Scanner(System.`in`)

val n = scan.nextLine().trim().toInt()

Since "in" is a Kotlin keyword

like image 199
UmAnusorn Avatar answered Nov 07 '22 09:11

UmAnusorn


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.

like image 28
s1m0nw1 Avatar answered Nov 07 '22 07:11

s1m0nw1