简体   繁体   English

什么是Kotlin中的Java Scanner?

[英]What is the equivalent of Java Scanner in Kotlin?

What is the equivalent of Java Scanner in Kotlin? 什么是Kotlin中的Java Scanner

I have used readLine() but I'd like to know whether it's type safe or not? 我使用过readLine()但我想知道它的类型是否安全?

You can try something like this 你可以尝试这样的事情

val scan = Scanner(System.`in`)

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

Since "in" is a Kotlin keyword 因为“in”是Kotlin关键字

Kotlin reuses many existing Java libraries and it's perfectly fine to do so with Scanner . Kotlin重用了许多现有的Java库,使用Scanner完全可以。 Otherwise, readLine simply uses System.in as you can observe here , which might be a simple alternative for you. 否则, readLine只是使用System.in ,你可以在这里观察,这可能是一个简单的替代方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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