- To take users input & print
User Input: 12
Output: 12
fun main(args: Array<String>) {
print("Enter a number : ")
val userInput = readLine()!!
var num:Int =userInput.toInt()
println("You entered : "+num)
}
OR
import java.util.Scanner
fun main(args: Array<String>) {
print("Enter a number : ")
val userInput = Scanner(System.`in`)
var num:Int =userInput.nextInt()
println("You entered : "+num)
}
Enter a number : 121 You entered : 121Program
val userInput = readLine()!! // !! - used to avoid null values
var num = userInput.toInt() //to take user input & convert it into integer
println("You entered : "+ num) //to print variable
We use readLine()
function to read userInput
OR
val userInput = Scanner(System.`in`)
var num = userInput.nextInt() //to take user input & convert it into integer
println("You entered : "+ num) //to print variable
For multiple user input prefer using Scanner()
method
But you must import java.util.Scanner in this case
Coming Soon !
QuickStrength does not come from winning. Your struggles develop your strengths.