Java Reference
In-Depth Information
2
Console Input and Output
Don't imagine you know what a computer terminal is. A computer termi-
nal is not some clunky old television with a typewriter in front of it. It is
an interface where the mind and the body can connect with the universe
and move bits of it about.
DOUGLAS ADAMS ,
Mostly Harmless
(the fifth volume in
The Hitchhiker's Trilogy
)
Introduction
This chapter covers simple output to the screen and input from the keyboard, often
called
console I/O
. We have already used console output, but this chapter covers it in
more detail. In particular, this chapter shows you how to format numerical output so
that you control such detail as the number of digits shown after the decimal point. This
chapter also covers the
Scanner
class, which was introduced in version 5.0 of Java and
can be used for console input.
console I/O
Prerequisites
This chapter uses material from Chapter 1 .
Let me tell the world.
WILLIAM SHAKESPEARE,
Henry IV
In this section, we review
System.out.println
and present some material on formatting
numeric output. As part of that material, we give a brief introduction to
packages
and
import statements
. Packages are Java libraries of classes. Import statements make classes
from a package available to your program.
We have already been using
System.out.println
for screen output. In Display 1.7 ,
we used statements such as the following to send output to the display screen:
System.out
println
System.out.println("The changed string is:");
System.out.println(sentence);
System.out
is an object that is part of the Java language, and
println
is a method
invoked by that object. It may seem strange to spell an object name with a dot in it, but
that need not concern us for now.