Taking inputs


#1

Most efficient way to input ints floats doubles and strings?

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


#2

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())
}


#3

https://habrahabr.ru/company/JetBrains/blog/222693/ suggests that this should work:

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

#4

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


#5

See https://stackoverflow.com/questions/41283393/reading-input-from-the-console-in-kotlin for more examples and tricks for parsing input from console and files in Kotlin.