Kotlin Basic Input/Output

Koltin Output

You can use println() and print() functions to send output to the standard output (screen). Let's take an example:

fun main(args : Array<String>) {
    println("Kotlin is interesting.")
}

When you run the program, the output will be:

Kotlin is interesting.

Here, println() outputs the string (inside quotes).


Difference Between println() and print()

  • print() - prints string inside the quotes.
  • println() - prints string inside the quotes similar like print() function. Then the cursor moves to the beginning of the next line.

When you use println() function, it calls System.out.println() function internally. (System.out.println() is used to print output to the screen in Java).

If you are using IntelliJ IDEA, put your 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 println() function is internally calling System.out.println().

Similarly, when you use print() function, it calls System.out.print() function.


Example 1: print() and println()

fun main(args : Array<String>) {
    println("1. println ");
    println("2. println ");

    print("1. print ");
    print("2. print");
}

When you run the program, the output will be:

1. println 
2. println 
1. print 2. print

Example 2: Print Variables and Literals

fun main(args : Array<String>) {
    val score = 12.3

    println("score")
    println("$score")
    println("score = $score")
    println("${score + score}")
    println(12.3)
}

When you run the program, the output will be:

score
12.3
score = 12.3
24.6
12.3

Kotlin Input

In this section, you will learn to take input from the user..

To read a line of string in Kotlin, you can use readline() function.


Example 3: Print String Entered By the User

fun main(args: Array<String>) {
    print("Enter text: ")

    val stringInput = readLine()!!
    println("You entered: $stringInput")
}

When you run the program, the output will be:

Enter text: Hmm, interesting!
You entered: Hmm, interesting!

It's possible to take input as a string using readLine() function, and convert it to values of other data type (like Int) explicitly.


If you want input of other data types, you can use Scanner object.

For that, you need to import Scanner class from Java standard library using:

import java.util.Scanner

Then, you need to create Scanner object from this class.

val reader = Scanner(System.`in`)

Now, the reader object is used to take input from the user.


Example 4: Getting Integer Input from the User

import java.util.Scanner

fun main(args: Array<String>) {

    // Creates an instance which takes 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 you run the program, the output will be:

Enter a number: -12
You entered: -12

Here, reader object of Scanner class is created. Then, the nextInt() method is called which takes integer input from the user which is stored in variable integer.


To get Long, Float, double and Boolean input from the user, you can use nextLong(), nextFloat(), nextDouble() and nextBoolean() methods respectively.

Did you find this article helpful?