Warning . Only one object should be linked to the keyboard or a file at a time. For example, do
not have two BufferedReaders br1 and br2 linked to the keyboard and inter-
leave expressions br1.readLine() and br2.readLine() . It will not always
work, which is worse than something that never works.
We can now check many test cases by executing the program and repeatedly typ-
ing a test case into the int field, hitting the ready button, and checking the out-
put in the String field.
SR1 . Fix MyJLiveWindow to have four int fields and four String fields. Fix
method buttonPressed so that it puts the integers 1 , 2 , 3 , 4 in the four int fields
and the four strings "one" , "two" , "three" , "four" in the four String fields.
SR2 . Fix MyJLiveWindow to have one int field and one String field. Fix
method buttonPressed so that it puts in the int field the length of the string in
the String field.
SR3 , Write a method with three parameters: a number of hours, minutes, and sec-
onds. The method should yield the total number of seconds given by the param-
eters. Test the method using GUI JLiveWindow .
Reading the keyboard and a file
The core Java language has no facilities for doing IO (input/output). Instead,
these facilities are provided in the API in package java.io . Here, we provide
enough information to allow you to read from the keyboard and from files.
The classes that will be used in this section are:
• InputStreamReader : An instance can read characters from the key-
• BufferedReader : An instance can read from any input, a line at a time.
• File : An instance is attached to a file in some directory on a hard drive.
• FileReader : An instance can read characters from a File .
• JFileChooser : An instance is a dialog window in which the user can
navigate to a directory and choose a file.
• FileOutputStream : An instance can write bytes to a file.
• PrintStream : An instance has print and println methods for writing
int s, double s, strings, etc.
The term stream is used for a sequence of data values that is processed —
either read or written— from beginning to end. When the data is being read, the
stream is called an input stream ; when it is being written, the stream is called an
output stream .