The when expressionIntroduction to KotlinCommand line argumentsReading from the terminal

Reading from the terminal

You already noticed the functions print and println for printing to the terminal.

To read input from the terminal, we use the readString() function. It prints a prompt, then waits for the user to enter a line. The result of the function is a string, so you may have to convert it to a number. Here is an example (drinks.kts):

import org.otfried.cs109.readString

var name = readString("What's your name? ").trim()
var answer = readString("Hi $name, would you like a beer? ").trim().toLowerCase()
if (answer == "yes" || answer == "y") {
   var num = readString("How many beer will you have? ").toInt()
   if (num > 5)
      println("That sounds a bit excessive!")
   else if (num < 2)
      println("Don't be shy...")
   else
      println("Here you are.")
}
The when expressionIntroduction to KotlinCommand line argumentsReading from the terminal