Skip to main content

Read Data Input using Scanner in Kotlin

2 min read Updated June 30, 2026
Share:
On this page (7sections)

Definition

  • Kotlin uses java.util.Scanner class to scan user inputs.
  • To read a string, nextLine() method is used.
  • Similarly, it has methods like nextInt(), nextLong() methods to get inputs of corresponding types.

Scanner Methods

Scanner scanner_variable = Scanner(System.`in`)

//Reading Int input
scanner_variable.nextInt()
//Reading Float Input
scanner_variable.nextFloat()
//Reading Double Input
scanner_variable.nextDouble()
//Reading Boolean Input
scanner_variable.nextBoolean()
//Reading Short Input
scanner_variable.nextShort()
//Reading Long Input
scanner_variable.nextLong()
//Reading String Input
scanner_variable.nextLine()

Syntax

Scanner scanner_variable = Scanner(System.`in`)
var variable_name = scanner_variable.nextLine()

For example,
var str = scanner.nextLine()

Read Data Input using Scanner Example Program in Kotlin

// Read Data Input in Kotlin example program
// Data Input Kotlin Programs, Basic Kotlin Programs

import java.util.Scanner

fun main(args: Array<String>) {

    val scanner  = Scanner(System.`in`)
    val num1 = scanner.nextInt()
    println("The input one is : $num1")
    val num2 = scanner.nextInt()
    println("The input two is : $num2")

    val sum = num1+num2

    println("The sum of the two inputs is : $sum")
}

Sample Output

Enter the inputs for addition
10
The input one is : 10
100
The input two is : 100
The sum of the two inputs is : 110

How It Works

Here is a step-by-step walkthrough of how the Kotlin program for Read Data Input using Scanner in Kotlin runs, line by line:

  1. fun main(args: Array<String>) { — defines a function used by the program.
  2. val scanner = Scanner(System.in) — reads a value entered by the user.
  3. val num1 = scanner.nextInt() — reads a value entered by the user.
  4. println("The input one is : $num1") — writes a line of output shown in the sample output.
  5. val num2 = scanner.nextInt() — reads a value entered by the user.
  6. println("The input two is : $num2") — writes a line of output shown in the sample output.
  7. val sum = num1+num2 — declares or assigns a value the program uses.
  8. println("The sum of the two inputs is : $sum") — writes a line of output shown in the sample output.

After running it, compare your console output with the Sample Output above. Try changing the values and re-running the program to see how the result changes — experimenting is the fastest way to understand the logic.

Frequently Asked Questions

What is Read Data Input using Scanner in Kotlin?
Kotlin uses java.util.Scanner class to scan user inputs.
How do I run this Kotlin example?
Run it in IntelliJ IDEA or Android Studio, or compile from the command line with `kotlinc file.kt -include-runtime -d file.jar` and run `java -jar file.jar`.
How can I practice Read Data Input using Scanner?
Copy the example into IntelliJ IDEA or Android Studio, run it, then change the values or add print statements to see how the output changes.

Related Tutorials

Search tutorials