简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM