Outdated Notice
To get ready to show for
loops, if
expressions, and other Scala constructs, let’s take a look at how to handle command-line input and output with Scala.
Writing output
As we’ve already shown, you write output to standard out (STDOUT) using println
:
println("Hello, world")
That function adds a newline character after your string, so if you don’t want that, just use print
instead:
print("Hello without newline")
When needed, you can also write output to standard error (STDERR) like this:
System.err.println("yikes, an error happened")
Because
println
is so commonly used, there’s no need to import it. The same is true of other commonly-used data types likeString
,Int
,Float
, etc.
Reading input
There are several ways to read command-line input, but the easiest way is to use the readLine
method in the scala.io.StdIn package. To use it, you need to first import it, like this:
import scala.io.StdIn.readLine
To demonstrate how this works, let’s create a little example. Put this source code in a file named HelloInteractive.scala:
import scala.io.StdIn.readLine
object HelloInteractive extends App {
print("Enter your first name: ")
val firstName = readLine()
print("Enter your last name: ")
val lastName = readLine()
println(s"Your name is $firstName $lastName")
}
Then compile it with scalac
:
$ scalac HelloInteractive.scala
Then run it with scala
:
$ scala HelloInteractive
When you run the program and enter your first and last names at the prompts, the interaction looks like this:
$ scala HelloInteractive
Enter your first name: Alvin
Enter your last name: Alexander
Your name is Alvin Alexander
A note about imports
As you saw in this application, you bring classes and methods into scope in Scala just like you do with Java and other languages, with import
statements:
import scala.io.StdIn.readLine
That import statement brings the readLine
method into the current scope so you can use it in the application.
Contributors to this page:
Contents
- Introduction
- Prelude꞉ A Taste of Scala
- Preliminaries
- Scala Features
- Hello, World
- Hello, World - Version 2
- The Scala REPL
- Two Types of Variables
- The Type is Optional
- A Few Built-In Types
- Two Notes About Strings
- Command-Line I/O
- Control Structures
- The if/then/else Construct
- for Loops
- for Expressions
- match Expressions
- try/catch/finally Expressions
- Scala Classes
- Auxiliary Class Constructors
- Supplying Default Values for Constructor Parameters
- A First Look at Scala Methods
- Enumerations (and a Complete Pizza Class)
- Scala Traits and Abstract Classes
- Using Scala Traits as Interfaces
- Using Scala Traits Like Abstract Classes
- Abstract Classes
- Scala Collections
- The ArrayBuffer Class
- The List Class
- The Vector Class
- The Map Class
- The Set Class
- Anonymous Functions
- Common Sequence Methods
- Common Map Methods
- A Few Miscellaneous Items
- Tuples
- An OOP Example
- sbt and ScalaTest
- The most used scala build tool (sbt)
- Using ScalaTest with sbt
- Writing BDD Style Tests with ScalaTest and sbt
- Functional Programming
- Pure Functions
- Passing Functions Around
- No Null Values
- Companion Objects
- Case Classes
- Case Objects
- Functional Error Handling in Scala
- Concurrency
- Scala Futures
- Where To Go Next