Taking inputs

Most efficient way to input ints floats doubles and strings?

val in: Scanner = Scanner(System.in) doesnt work

1 Like

This question seems to target Java in general, not a Kotlin specific problem. At least Scanner works for me without System.in.

import java.util.Locale import java.util.Scanner

fun main(args: Array<String>) {
  Locale.setDefault(Locale.ENGLISH)
  println(Scanner(“42”).nextInt())
  println(Scanner(“42”).nextDouble())
  println(Scanner(“42.3”).nextDouble())
  println(Scanner(“true”).nextBoolean())
}

Мы провели Kotlin Challenge: что в финале? / Habr suggests that this should work:

val input = Scanner(System.`in`)
val T = input.nextInt()

Yes. in is a keyword and the compiler complains with "expecting property name or receiver type" and "expecting an element".

See Reading console input in Kotlin - Stack Overflow for more examples and tricks for parsing input from console and files in Kotlin.