This post will discuss how to read input from the console in Kotlin.

1. Using Scanner

A simple solution is to use the Scanner class for reading input from the console. It splits the input into tokens using whitespace as a separator, which then can be converted into different data types with the corresponding .next*() functions:

Download Code

2. Using BufferedReader

We can achieve better performance using the BufferedReader class. The idea is to read the whole line as a string using the readLine() function, and convert it into different data types using the standard conversion functions provided by the String class.

Download Code

 
If the input is an integer, we can directly call the read() function, as shown below.

Download Code

3. Using Console

Finally, we can get access to the character-based console to read input from it. The System.console() function returns the Console object associated with the current Java virtual machine.

Download Code

That’s all about reading input from the console in Kotlin.