How to prompt users for input from Scala shell scripts

This is an excerpt from the Scala Cookbook, partially modified for the internet. This is Recipe 14.12, “How to prompt users for input from Scala shell scripts.”

Back to top


You want to prompt a user for input from a Scala shell script and read her responses.

Back to top


Use the readLine, print, printf, and* methods to read user input, as demonstrated in the following script. Comments in the script describe each method:

Table of Contents

  1. Problem
  2. Solution
Back to top

How to read interactive command-line input with Java

Java command-line FAQ: How do I read command line input from a Java application (interactively)?

Solution: As of Java 5 (and newer Java versions), the best way to solve this problem is to use the Java Scanner class. Before showing an example of how to use the Scanner class, here’s a short description of what it does from the Javadoc: