English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this article, you will learn how to display output on the screen and accept user input in Kotlin.
You can use the println() and print() functions to send output to standard output (the screen). Let's take an example:
fun main(args : Array<String>) { println("Kotlin is very interesting.") }
When running the program, the output is:
Kotlin is very interesting.
Here, println() outputs the string (within the quotes).
print() - Print the string within the quotes.
println() - Print the string within the quotes, similar to the print() function. Then the cursor moves to the beginning of the next line.
When you use the println() function, it internally calls the System.out.println() function. (In Java, System.out.println() is used to print output to the screen).
If you are using IntelliJ IDEA, place the mouse cursor next to println and go to Navigate > Declaration (shortcut: Ctrl) + B. For Mac: Cmd + B) , this will open Console.kt (declaration file). You can see that the println() function internally calls System.out.println().
Similarly, when using the print() function, it calls the System.out.print() function.
fun main(args : Array<String>) { println("1. println "); println("2. println "); print("1. print "); print("2. print"); }
When running the program, the output is:
1. println 2. println 1. print 2. print
fun main(args : Array<String>) { val score = 12.3 println("score") println("$score") println("score = $score") println("${score} + score") println(12.3) }
When running the program, the output is:
score 12.3 score = 12.3 24.6 12.3
In this section, you will learn to accept input from the user.
To read a line of string in Kotlin, you can use the readline() function.
fun main(args: Array<String>) { print("Input text: ") val stringInput = readLine()!! println("You entered: ") }
When running the program, the output is:
Input text: Hmm, interesting! You entered: Hmm, interesting!
You can use the readLine() function to take input as a string and explicitly convert it to a value of other data types (such as Int).
If you want to input other data types, you can use the Scanner object.
To do this, you need to use the following command to import the Scanner class from the Java standard library:
import java.util.Scanner
Then, you need to create a Scanner object from this class.
val reader = Scanner(System.`in`)
Now, the reader object is used to get input from the user.
import java.util.Scanner fun main(args: Array<String>) { // Create an instance that reads input from standard input (keyboard) val reader = Scanner(System.`in`) print("Enter a number: ") //nextInt() reads the next integer from the keyboard var integer: Int = reader.nextInt() println("You entered: $integer") }
When running the program, the output is:
Enter a number: -12 You entered: -12
Here, an object of the Scanner class named reader is created. Then, the nextInt() method is called, which retrieves an integer input from the user and stores it in the variable integer.
To obtain input of type Long, Float, Double, and Boolean from the user, you can use the nextLong(), nextFloat(), nextDouble(), and nextBoolean() methods, respectively.