Open In App

Kotlin Standard Input/Output

Last Updated : 04 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In this article, we will discuss how to take input and how to display the output on the screen in Kotlin. Kotlin standard I/O operations are performed to flow a sequence of bytes or byte streams from an input device, such as a Keyboard, to the main memory of the system and from main memory to an output device, such as a 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 Output

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 functions to display output on the screen.

print() function    // prints text
println() function // prints text and then moves the cursor to a new line.

Here is the Kotlin program for standard output.  

Example:

Kotlin
fun main(args: Array<String>)
{
    print("Hello, Geeks! ")
    println("This is Kotlin tutorial.")
    print("By GFG!")
}


Output: 

Hello, Geeks! This is Kotlin tutorial.
By GFG!


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 a string:  

Example:

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(). 


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 Input

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 the user with the help of the following function:  

readline() method
Scanner class

Take input from the user using the 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 the user using the Scanner class:

If you are taking input from the 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 the 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 follow it with (!!) 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



Next Article
Article Tags :

Similar Reads