14.12. Prompting for Input from a Scala Shell Script
You want to prompt a user for input from a Scala shell script and read her responses.
Console.read* methods to read user input, as
demonstrated in the following script. Comments in the script describe
// write some text out to the user with Console.println
// Console is imported by default, so it's not really needed, just use println
// readLine lets you prompt the user and read their input as a String
"What's your name? "
// readInt lets you read an Int, but you have to prompt the user manually
"How old are you? "
// you can also print output with printf
"Your name is $name and you are $age years old."
readLine method lets you
prompt a user for input, but the other
read* methods don’t, so you need to prompt the
user manually with
You can list the
methods in the Scala REPL:
Console.readreadBoolean readByte readChar readDouble readFloat readInt readLine readLong readShort readf readf1 readf2 readf3
Be careful with the methods that read numeric values; as you might
expect, they can all throw a
Although these methods are thorough, if you prefer, you can also
fall back and read input with the Java
// you can also use the Java Scanner class, if ...