We have the scores in a file
called Students, and each line in the file contains a single score. Each score is the grade of one of the students
(e.g., 89.5). We could write code such as this to read the scores from the file and compute the total of
all the scores:
double total = 0.0;
BufferedReader in = new BufferedReader(
new FileReader( "Students" ) );
for ( int i = 1; i <= numberOfStudents; i++ ) {
String score = in.readLine();
total = total + Double.parseDouble( score );
}
72 PROGRAMMING IN JAVA [CHAP. 5
The first line declares a thing called a BufferedReader, which is ???wrapped around??? a FileReader
that opens the file called Students. We will discuss these sorts of input/output statements further in the
section on Input and Output. This statement declares the variable in to be a BufferedReader associated with
the file Students.
Notice that programming statements in Java can continue to a second, third, or more lines. The Java
compiler will continue to interpret the lines as one statement until it encounters the semicolon, which marks the
end of the statement.
The for statement begins with parentheses enclosing three expressions, which are separated by semicolons.
The first expression defines initial conditions in the for loop.
Pages:
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202