print(...) to print something without a newline, and println(...) to
print with a newline at the end. println() with no args to start a new
line.
Nothing built in to Kotlin to print to stderr. Use Java functions when running on the JVM.
System.err.println("Error!")
Use available Java logging libraries. Generally will look like this:
class MyClass {
companion object {
val LOG = Logger.getLogger(MyClass::class.java.name)
}
fun foo() {
LOG.warning("Uh-oh")
}
}
In Android:
Log.w("Something bad")
fun main(args: Array<String>) {
println(args.size)
println(args.contentToString())
}
Output from input “x y z”:
3
[x, y, z]
val input = readln()
val i = readln().toIntOrNull()
Use readlnOrNull() to return null if end of input is reached, otherwise
readln() throws an exception.
Usually it is best to use a Java Scanner.
import java.util.Scanner
val scanner = Scanner(System.`in`)
val line = scanner.nextLine()
val i = scanner.nextInt()
scanner.close()
Call close when done or take advantage of AutoCloseable resource.
Scanner(System.`in`).use { scanner ->
//...
}