esu
esu

Reputation: 2450

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?

Upvotes: 14

Views: 23254

Answers (2)

s1m0nw1
s1m0nw1

Reputation: 81859

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 see here. This might be a simple alternative for you.

Upvotes: 8

UmAnusorn
UmAnusorn

Reputation: 11124

You can try something like this

val scan = Scanner(System.`in`)

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

Since "in" is a Kotlin keyword

Upvotes: 17

Related Questions