Kareem Mohamed
Kareem Mohamed

Reputation: 11

Take input in Kotlin

how can i take an int or float or any numeric input from user except String because .readline() only take String as an input . for example i want to take numbers from user in this example:

println"Enter your age"

var age:int =readInt()

This gives me an error

Upvotes: 1

Views: 1277

Answers (4)

Bobby Sharif
Bobby Sharif

Reputation: 1

Input Int in Kotlin var input:Int=Integer.valueOf(readLine())

Upvotes: 0

Lucas Rausch
Lucas Rausch

Reputation: 3

To get input from a user, try:

fun main(args: Array<String>) {
    val input = readLine()
    println(input)
}

Hope I helped!

Upvotes: 0

Jo&#227;o Dias
Jo&#227;o Dias

Reputation: 17500

You can also do the following:

fun main() {
    with(Scanner(System.`in`)) {
       val a = nextInt()
       println(a)
    }
}

Upvotes: 0

Jagadeesh K
Jagadeesh K

Reputation: 886

fun main() {
  val num = readLine()!!
  println(num.toInt())
}

You can read the input as string and then convert it to the data type you want later

Upvotes: 1

Related Questions