Standard input Standard input is a stream of data that goes into the program. By default, standard input gets data from the keyboard but it is possible to get it from a file. Kotlin functions Kotlin has a useful function to read data from the standard input. It is supported by the operating system. It is readline . It reads the whole line as a string. fun main () { val line = readLine ()!! println (line) } If you need to get a number from the input, you can use this construction: fun main () { val line = readLine ()?. toInt ()!! println (line) } To accept multiple words in a single use the following code val (a , b) = readLine ()!!. split ( ' ' ) println ( " $ a , $ b " ) val (c , d , e) = readLine ()!!. split ( ' ' ) println ( " $ c , $ d , $ e " ) Java Scanner Another way to obtain data from the standard input is to use the Java Scanner. Scanner allows a program to read values of different types (strings, numbers, etc) from ...
Comments
Post a Comment