Read String form Console Input – Scala

If you are developing a console application with Scala, you may need to read input from the user.

In this tutorial, we will learn how to read a line from the console as input from a user.

To read a line from console input, we use the method scala.io.StdIn.readLine().

Example 1 – Read String from Console as Input

ADVERTISEMENT

In this example, we shall read a string from console and print it to the console.

example.scala

object ReadInputExample {
  def main(args: Array[String]) {
    val name = scala.io.StdIn.readLine("Enter your name: ")
    println("Hello "+name)
  }
}

Output

Enter your name: Arjun
Hello Arjun