How to use java.util.Scanner In Scala
Below is the Scala program to take an input from user:
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:
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