Input

Silly question: how can i read from standard input (not arguments line)? Thx, regards.

readLine():

http://jetbrains.github.com/kotlin/versions/snapshot/apidocs/kotlin/io/package-summary.html#readLine()

And, of course, System.in is still there for you.

Kotlin standard library contans println and readLine functions which could help in solving you task gracefully:

``

fun main(args: Array<String>) {
  println(“Line: ${readLine()}”) // Read line and print it back to console
}

In fact under the hood those functions are simple wrappers for java.io, and as kotlin is compatible with Java you can also choose to implement same program like this:

``

import java.io.InputStreamReader
import java.io.BufferedReader

fun main(args: Array<String>) {
  val reader = BufferedReader(InputStreamReader(System.in))
  System.out.println(“Line: ${reader.readLine()!!}”)
}

1 Like

Ok. I've tried with readLine() but i got

"C:Program FilesJavajdk1.7.0binjava" -Didea.launcher.port=7533 -Didea.launcher.bin.path=D:intellijbin -Dfile.encoding=UTF-8 -classpath "C:Program FilesJavajdk1.7.0jrelibcharsets.jar;C:Program FilesJavajdk1.7.0jrelibdeploy.jar;C:Program FilesJavajdk1.7.0jrelibjavaws.jar;C:Program FilesJavajdk1.7.0jrelibjce.jar;C:Program FilesJavajdk1.7.0jrelibjsse.jar;C:Program FilesJavajdk1.7.0jrelibmanagement-agent.jar;C:Program FilesJavajdk1.7.0jrelibplugin.jar;C:Program FilesJavajdk1.7.0jrelibresources.jar;C:Program FilesJavajdk1.7.0jrelibrt.jar;C:Program FilesJavajdk1.7.0jrelibextdnsns.jar;C:Program FilesJavajdk1.7.0jrelibextlocaledata.jar;C:Program FilesJavajdk1.7.0jrelibextsunec.jar;C:Program FilesJavajdk1.7.0jrelibextsunjce_provider.jar;C:Program FilesJavajdk1.7.0jrelibextsunmscapi.jar;C:Program FilesJavajdk1.7.0jrelibextzipfs.jar;D:untitledoutproductionuntitled;D:untitledlibkotlin-runtime.jar;D:intellijlibidea_rt.jar" com.intellij.rt.execution.application.AppMain namespace Exception in thread "main" java.lang.IllegalAccessError: tried to access field kotlin.io.namespace.stdin from class kotlin.io.namespace$src$1 at kotlin.io.namespace$src$1.readLine(JIO.kt:144) at kotlin.io.namespace.readLine(Files.kt) at namespace.main(a1.kt:2) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:601) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

reading further i saw there was a similar issue marked as a bug.... so i was looking for alternatives.

tk u.

Thx, this way (that is using java.io without wrapping it) has no problems and works fine.

we can take input in Kotlin via Scanner Class of Java:slightly_smiling_face:

import java.util.Scanner;

fun main(args: Array<String){
    var value =Scanner(System.`in`);
    println("Enter name")

    val data =value.next()
        println("$data")
}