33. App 2: Read User Input (Introduction to Functional Programming)

Опубликовано: 17 Июнь 2026
на канале: Ziverge
6
0

To read input from a user, we use the readLine function that’s in the scala.io.StdIn data type. Again — imagining that we’re reading input in a REST service — we know that we want to return a Try:

def readInput(): Try

Because readLine returns a String, the function returns a Try[String], and the complete function looks like this:

def readInput(): Try[String] = Try {
StdIn.readLine()
}