Kotlin Standard Input/Output

04 Aug, 2021
In this article, we will discuss here how to take input and how to display the output on the screen in Kotlin. Kotlin standard I/O operations are performed to flow sequence of bytes or byte streams from input device such as Keyboard to the main memory of the system and from main memory to output device such as Monitor. 
In Java, we use System.out.println(message) to print output on the screen but, in Kotlin println(message) is used to print. 
Kotlin standard output is the basic operation performed to flow byte streams from main memory to the output device. You can output any of the data types integer, float and any patterns or strings on the screen of the system. You can use any one of the following function to display output on the screen.  
print() function 
println() function
Here is the Kotlin program for standard output.  
Kotlin
fun main(args: Array<String>){    print("Hello, Geeks! ")    println("This is Kotlin tutorial.")}
Output: 
Hello, Geeks! This is Kotlin tutorial.
Difference between println() and print() – print() function prints the message inside the double quotes. println() function prints the message inside the double quotes and moves to the beginning of the next line.
kotlin program to print string:  
Kotlin
fun main(args         : Array<String>){    println("GeeksforGeeks")    println("A Computer Science portal for Geeks")     print("GeeksforGeeks - ")    print("A Computer Science portal for Geeks")}
Output: 
GeeksforGeeks
A Computer Science portal for Geeks
GeeksforGeeks - A Computer Science portal for Geeks
Actually print() internally calls System.out.print() and println() internally calls System.out.println(). If your are using Intellij IDEA, then click next to println and Go To > Declaration by clicking the right button. (Shortcut: in Window Press Ctrl + B and in Mac press Cmd + B). It will open Console.kt file and you can see that internally it calls System.out.println(). 
Print literals and Variables –  
Kotlin
fun sum(a: Int,b: Int) : Int{    return a + b}fun main(args: Array<String>){     var a = 10    var b = 20    var c = 30L    var marks = 40.4     println("Sum of {$a} and {$b} is : ${sum(a,b)}")    println("Long value is: $c")    println("marks")    println("$marks")}
Output: 
Sum of {10} and {20} is : 30
Long value is: 30
marks
40.4 
Kotlin standard input is the basic operation performed to flow byte streams from input device such as Keyboard to the main memory of the system. 
You can take input from user with the help of the following function:  
readline() method
Scanner class
Take input from user using readline() method –  
Kotlin
fun main(args : Array<String>) {    print("Enter text: ")    var input = readLine()    print("You entered: $input")}
Output: 
Enter text: Hello, Geeks! You are learning how to take input using readline()
You entered: Hello, Geeks! You are learning how to take input using readline()
Take input from user using Scanner class – If you are taking input from user other than String data type, you need to use Scanner class. To use Scanner first of all you have to import the Scanner on the top of the program.  
import java.util.Scanner;
Create an object for the scanner class and use it to take input from the user. In the below program we will show you how to take input of integer, float and boolean data type. 
Kotlin
import java.util.Scanner fun main(args: Array<String>) {         // create an object for scanner class    val number1 = Scanner(System.`in`)          print("Enter an integer: ")    // nextInt() method is used to take    // next integer value and store in enteredNumber1 variable    var enteredNumber1:Int = number1.nextInt()    println("You entered: $enteredNumber1")     val number2 = Scanner(System.`in`)    print("Enter a float value: ")     // nextFloat() method is used to take next    // Float value and store in enteredNumber2 variable    var enteredNumber2:Float = number2.nextFloat()    println("You entered: $enteredNumber2")     val booleanValue = Scanner(System.`in`)    print("Enter a boolean: ")    // nextBoolean() method is used to take    // next boolean value and store in enteredBoolean variable    var enteredBoolean:Boolean = booleanValue.nextBoolean()    println("You entered: $enteredBoolean")}
Output: 
Enter an integer: 123
You entered: 123
Enter a float value:  40.45
You entered: 40.45
Enter a boolean:  true
You entered: true
Take input from user without using the Scanner class – Here, we will use readline() to take input from the user and no need to import Scanner class. 
readline()!! take the input as a string and followed by (!!) to ensure that the input value is not null. 
Kotlin
fun main(args: Array<String>) {     print("Enter an Integer value: ")    val string1 = readLine()!!      // .toInt() function converts the string into Integer    var integerValue: Int = string1.toInt()    println("You entered: $integerValue")     print("Enter a double value: ")    val string2= readLine()!!     // .toDouble() function converts the string into Double    var doubleValue: Double = string2.toDouble()    println("You entered: $doubleValue")}
Output: 
Enter an Integer value: 123
You entered: 123
Enter a double value:  22.22222
You entered: 22.22222
 
surindertarika1234
Kotlin Basics
Kotlin
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Kotlin constructor
Retrofit with Kotlin Coroutine in Android
Android Menus
Kotlin Higher-Order Functions
Suspend Function In Kotlin Coroutines
Spinner in Kotlin
MVP (Model View Presenter) Architecture Pattern in Android with Example
Kotlin extension function
How to Get Current Location in Android?
Launch vs Async in Kotlin Coroutines