![]() |
VOOZH | about |
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.
Table of Content
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:
Output:
Hello, Geeks! This is Kotlin tutorial.
By GFG!
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:
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().
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 the user with the help of the following function:
readline() method
Scanner class
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()
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.
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
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.
Output:
Enter an Integer value: 123
You entered: 123
Enter a double value: 22.22222
You entered: 22.22222