in , ,

Kotlin Basic Input/Output

Kotlin Basic InputOutput
Kotlin Basic InputOutput

Kotlin Input Output: In this tutorial, you will learn to display output to the screen, and take input from the user in Kotlin.

Kotlin Input Output

Kotlin Input-Output statements are used to read input stream from any standard input gadget (ex. keyboard) to fundamental memory and to send a data stream to any standard output gadget (ex. monitor).

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 comparable like print() function. At that point, the cursor moves to the start of the next line.

At the point when you use println() function, it calls System.out.println() function inside. (System.out.println() is used to print output to the screen in Java).

On the off chance that 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 inside calling System.out.println().

Additionally, 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");
}

At the point 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)
}

At the point 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() work.


Example 3: Print String Entered By the User

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

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

At the point when you run the program, the output will be:

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

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


In the event that you need 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

At that point, you need to create Scannerobject from this class.

val reader = Scanner(System.`in`)

Presently, 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")
}

At the point when you run the program, the output will be:

Enter a number: -12
You entered: -12

Here, the reader object of the Scanner class is created. At that point, the nextInt() technique is called which takes integer input from the user which is put away in variable integer.


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


Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.

salman khan

Written by worldofitech

Leave a Reply

Kotlin Comments

Kotlin Comments

Kotlin if Expression

Kotlin if Expression