Introduction
Input/output is a crucial part that makes any program interactive. Keyboards are the standard devices to send input to the program, and the output of the program is made visible with the help of another standard device known as Monitor.
So in this blog, we will discuss the basic input/output operations in Kotlin. An input is a stream of bytes/data that flows from the keyboard to the CPU. Similarly, the output is a stream of bytes that flows from the CPU towards the monitor.
Kotlin Input
We can take input from a user in a Kotlin program with the help of the following functions:
→ readline() method
→ Scanner class
Taking input using readline() method
// Kotlin program to demonstrate taking input from a user through readLine() function
fun main() {
print("Enter input text: ")
var input_text = readLine()
print("The entered text is: $input_text")
}
Input:
Enter input text: Welcome to Coding Ninjas
Output:
The entered text is: Welcome to Coding Ninjas
Taking input using Scanner class
The readLine() function helps us take an input of only String data type. To take inputs of other data types, you can use the Scanner class.
Import the Scanner at the top of the program as depicted below:
import java.util.Scanner
For taking input from the user, you need to create an object of the Scanner class, as shown in the program below. In this example we will take input as an integer, float and boolean using the nextInt(), nextFloat() and nextBoolean() functions respectively. Similarly, we can use nextLong() and nextDouble() methods to take long and double inputs, respectively.
import java.util.Scanner
// Kotlin program to demonstrate input/output operations
fun main() {
// Create an object of scanner class
val num1 = Scanner(System.`in`)
print("Enter an integer: ")
// Using nextInt() method
var num_1:Int = num1.nextInt()
println("The entered value is: $num_1")
val num2 = Scanner(System.`in`)
print("Enter a float value: ")
// Using nextFloat() method
var num_2:Float = num2.nextFloat()
println("The entered value is: $num_2")
val boolval = Scanner(System.`in`)
print("Enter a boolean: ")
// Using nextBoolean() method
var bool_val:Boolean = boolval.nextBoolean()
println("The entered value is: $bool_val")
}
Input:
Enter an integer: 715
Enter a float value: 39.39
Enter a boolean: false
Output:
The entered value is: 715
The entered value is: 39.39
The entered value is: false
Taking input without using the Scanner class
There is another way to read an integer or double value without using the Scanner class. We can read the input as a String using readLine() function and then convert the String into an int or double using the toInt() and toDouble() functions. Also, to ensure a non-null value, the readLine() function is followed by two exclamation marks.
readline()!! - It ensures a non-null value is taken as input.
// Kotlin program to demonstrate reading input from the user and changing the data type
fun main() {
print("Enter an Integer value: ")
val input1 = readLine()!!
// Using toInt() function to convert String into Double
var num: Int = input1.toInt()
println("The entered value is: $num")
print("Enter a double value: ")
val input2= readLine()!!
// Using toDouble() function to convert String into Double
var double: Double = input2.toDouble()
println("The entered value is: $double")
}
Input:
Enter an Integer value: 797
Enter a double value: 56.34
Output:
The entered value is: 797
The entered value is: 56.34




