256
them in the instantiation of objects of the other two classes.BufferedReaderprovides a
readLinemethod that inputs an entire line as a string.PrintWriterprovides theprint
andprintlnmethods that enable the output of the standard Java primitive types and
theStringclass.
To use files, you must do five things: (1) import the package java.io.*, (2) declare
the file variables along with your other variable declarations, (3) instantiate each file
object, (4) use methods associated with each file object to read or write it, and (5) call
the closemethod associated with each file object when you are finished with it.
When using files, we must forward exceptions to the JVM by adding a throws
IOExceptionclause to the heading of main.
The whilestatement is a looping construct that allows the application to repeat a
statement as long as the value of an expression remains true. When the value of the
expression becomes false, the statement is skipped, and execution continues with
the first statement following the loop.
With the whilestatement, you can construct several types of loops that you can
use again and again. These types of loops are classified into two categories: count-
controlled loops and event-controlled loops.
In a count-controlled loop, the loop body is repeated a specified number of times.
You initialize a counter variable immediately before the whilestatement. This
variable is the loop control variable. The control variable is tested against the limit in
the whileexpression. The last statement in the loop body increments the control
variable.
Event-controlled loops continue executing until something inside the body
signals that the looping process should stop. Event-controlled loops include those
that test for a sentinel value in the data, end-of-file, or a change in a flag variable.
Sentinel-controlled loops are input loops that use a special data value as a signal
to stop reading. EOF-controlled loops are loops that continue to input (and process)
data values until no more data remain. To implement them with a whilestatement,
you must test the value returned by the input method. The readLinemethod returns
null. Sentinel-controlled loops usually require a priming read just before entry into
the loop and an updating read at the end of the loop. You can use Java’s assignment
expression as a shortcut to combine these two input operations into one within the
loop test. The assignment expression shortcut should be used only in simple cases
where the intent is clear to a human reader. Otherwise, it is preferable to write a sen-
tinel-controlled loop in the usual manner.
Counting is a looping operation that keeps track of how many times a loop is
repeated or how many times some event occurs. This count can be used in computa-
tions or to control the loop. A counter is a variable that is used for counting. It may
be the loop control variable in a count-controlled loop, an iteration counter in a
counting loop, or an event counter that counts the number of times a particular con-
dition occurs in a loop.