0

Note: Please do not downvote question as I tried searching but did not find anything. The question has already been marked duplicate.

How to read primitive datatype values in Kotlin?

We can use Scanner object of java but I want to implement using readLine function of kotlin.

How do I scan numbers e.g num1 and num2 and perform some operation like sum ?

How do it convert following code to koltin without using scanner?

val sc = Scanner(System.in)
val num1 = sc.nextInt()
val num2 = sc.nextInt()
val sum = sum(num1, num2)
Akshar Patel
  • 8,998
  • 6
  • 35
  • 50
  • 1
    Take also a look at this, it has a lot of examples :) https://stackoverflow.com/questions/41283393/reading-input-from-the-console-in-kotlin – developer_hatch Jun 05 '17 at 14:08

1 Answers1

1

Just do something like:

fun main(vararg args: String) {
  val (a, b) = readLine()!!.split(' ')
  println(a.toInt() + b.toInt())
}
developer_hatch
  • 15,898
  • 3
  • 42
  • 75