The standard input is a stream of data that goes into a program. It is supported by the operating system. By default, the standard input obtains data from the keyboard, but it is also possible to get it from a file.
Actually, not all programs need to use the standard input, but we will use it often. The typical way to solve programming problems in this course is the following:
1) read data from the standard input (stdin);
2) process data to obtain a result
3) output the result to the standard output (stdout)
Before we start writing programs that do something useful, you should understand how to read data from the standard input.
Using the readLine function
Kotlin has one useful function to read data from the standard input. This function is called
readLine
. It reads the whole line as a string.val line = readLine()
The line variable has the String type because the function returns a value of this type.
Here is a program that reads a line from the standard input and send to the standard output.
fun main(args: Array<String>) {
val line = readLine()
println(line)
}
Here is an example of valid input data:
Hyperskill
The output is:
Hyperskill
This function has a number of shortcomings. If you'd like to read several values from the same line one by one, then you need to perform additional actions.
Using Java scanner
The simplest way to obtain data from the standard input is to use the standard type
Scanner
from Java. It can be directly accessed from Kotlin because it is an interoperabile with Java libraries. A scanner allows a program to read values of different types (strings, numbers, etc) from the standard input.To use this class you should add the following import statement to the top of your file with the source code.
import java.util.Scanner
or
import java.util.*
Then you can use any of the following methods to add Scanner
to your program. We will discuss importing in more detail later.Let's create a variable initialized by
Scanner
:val scanner = Scanner(System.`in`)
Note,
System.`in`
is an object that represents the standard input stream.Now we can read data from the standard input:
val line = scanner.nextLine() // read a whole line, for example "Hello, Kotlin"
val num = scanner.nextInt() // read a number, for example 123
val string = scanner.next() // read a string (not a line), for example "Hello"
After you call
scanner.nextLine()
or scanner.nextInt()
or something similar, the program will wait for the input data.Here is an example of correct input data:
Hello, Kotlin
123 Hello
The following input example is also correct:
Hello, Kotlin
123
Hello
It's possible to read a number as a string using
scanner.next()
or scanner.nextLine()
(if the number is in a new line).Also, the class
Scanner
has methods (i.e. functions) for reading some values of other types. See the documentation for details.The following program reads two numbers and outputs them in reverse order in two different lines.
import java.util.Scanner // a class (type) from the Java standard library
fun main(args: Array<String>) {
val scanner = Scanner(System.`in`) // reads data
val num1 = scanner.nextInt() // reads the first number
val num2 = scanner.nextInt() // reads the second number
println(num2) // prints the second number
println(num1) // prints the first number
}
Example 1
Input:
11 12
Output:
12
11
Example 2
Input:
301
402
Output:
402
301
We recommend you use the
readLine
function or the class Scanner
when solving programming problems in this course. They are the simplest ways to get values from the standard input.