How to use java.util.Scanner In Scala

Below is the Scala program to take an input from user:

Scala
import java.util.Scanner

object UserInputExample {
  def main(args: Array[String]): Unit = {
    val scanner = new Scanner(System.in)
    println("Enter your name:")
    val name = scanner.nextLine()
    println("Hello, " + name + " Welcome!")
  }
}

Output:

java.util.Scanner Output

Explanation:

In this example, we import java.util.Scanner and use it to read input from the standard input stream using nextLine() method.

How to take Input from a User in Scala?

This article focuses on discussing various approaches to taking input from a user in Scala.

Table of Content

  • Using scala.io.StdIn.readLine() Method
  • Using java.util.Scanner
  • Using Command-line Argument

Similar Reads

Using scala.io.StdIn.readLine() Method

Below is the Scala program to take input from the user:...

Using java.util.Scanner

Below is the Scala program to take an input from user:...

Using Command-line Argument

Below is the Scala program to take an input from user:...