Java Reference
In-Depth Information
nation which the user enters to indicate that there's
no more data to input
. In Chapter 15,
Files, Streams and Object Serialization, you'll see how the end-of-file indicator is used
when a program reads its input from a file.
On UNIX/Linux/Mac OS X systems, end-of-file is entered by typing the sequence
<Ctrl> d
on a line by itself. This notation means to simultaneously press both the
Ctrl
key and the
d
key. On Windows systems, end-of-file can be entered by typing
<Ctrl> z
[
Note:
On some systems, you must press
Enter
after typing the end-of-file key sequence.
Also, Windows typically displays the characters
^Z
on the screen when the end-of-file in-
dicator is typed, as shown in the output of Fig. 5.9.]
Portability Tip 5.1
The keystroke combinations for entering end-of-file are system dependent.
The
while
statement (lines 26-56) obtains the user input. The condition at line 26
calls
Scanner
method
hasNext
to determine whether there's more data to input. This
method returns the
boolean
value
true
if there's more data; otherwise, it returns
false
.
The returned value is then used as the value of the condition in the
while
statement.
Method
hasNext
returns
false
once the user types the end-of-file indicator.
Line 28 inputs a grade value from the user. Line 29 adds
grade
to
total
. Line 30
increments
gradeCounter
. These variables are used to compute the average of the grades.
Lines 33-55 use a
switch
statement to increment the appropriate letter-grade counter
based on the numeric grade entered.
Processing the Grades
The
switch
statement (lines 33-55) determines which counter to increment. We assume
that the user enters a valid grade in the range 0-100. A grade in the range 90-100 repre-
sents A, 80-89 represents B, 70-79 represents C, 60-69 represents D and 0-59 represents
F. The
switch
statement consists of a block that contains a sequence of
case
labels
and
an optional
default
case
. These are used in this example to determine which counter to
increment based on the grade.
When the flow of control reaches the
switch
, the program evaluates the expression in
the parentheses (
grade / 10
) following keyword
switch
. This is the
switch
's
controlling
expression
. The program compares this expression's value (which must evaluate to an inte-
gral value of type
byte
,
char
,
short
or
int
, or to a
String
) with each
case
label. The con-
trolling expression in line 33 performs integer division, which
truncates the fractional part
of the result. Thus, when we divide a value from 0 to 100 by 10, the result is always a value
from 0 to 10. We use several of these values in our
case
labels. For example, if the user
enters the integer
85
, the controlling expression evaluates to 8. The
switch
compares
8
with each
case
label. If a match occurs (
case
8:
at line 40), the program executes that
case
's statements. For the integer
8
, line 41 increments
bCount
, because a grade in the 80s
is a B. The
break
statement
(line 42) causes program control to proceed with the first
statement after the
switch
—in this program, we reach the end of the
while
loop, so con-