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 uni-
verse 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 been using console output, but this chapter cov-
ers it in more detail. In particular, this chapter shows you how to format numerical
output so that you control such details as the number of digits shown after the decimal
point. This chapter also covers the
console I/O
class, which is new in version 5.0 of Java
Scanner
and which can be used for console input.
Prerequisites
This chapter uses material from Chapter 1.
2.1
Screen Output
Let me tell the world.
WILLIAM SHAKESPEARE,
Henry IV
and present some material on format-
ting numeric output. As part of that material, we give a brief introduction to
In this section, we review
System.out.println
packages
and
. Packages are Java libraries of classes. Import statements make
classes from a package available to your program.
import statements
System.out.println
System.out.
println
for screen output. In Display 1.7, we
used statements such as the following to send output to the display screen:
We have already been using
System.out.println
System.out.println("The changed string is:");
System.out.println(sentence);
Search WWH ::




Custom Search