Taking User Input in Kotlin

To take the user input we import java file “java.util.Scanner”

Kotlin
import java.util.Scanner
fun main(){  
}

Now in main function make a variable which will store a function calling as “Scanner(System.`in`)”

Kotlin
import java.util.Scanner
fun main(){
    val input = Scanner(System.`in`)
}

Now to take input in variable as :

1 Int type :

Kotlin
import java.util.Scanner
fun main(){
    val input = Scanner(System.`in`)
    val x = input.nextInt()
}

2 String type :

Kotlin
import java.util.Scanner
fun main(){
    val input = Scanner(System.`in`)
    val x = input.next()
    print(x)
}

Leave a Comment