1 chapter 5 file objects and looping statements. 2 chapter 9 topics l using data files for i/o l...
Post on 21-Dec-2015
223 views
TRANSCRIPT
![Page 1: 1 Chapter 5 File Objects and Looping Statements. 2 Chapter 9 Topics l Using Data Files for I/O l While Statement Syntax l Count-Controlled Loops l Event-Controlled](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649d5f5503460f94a3eea9/html5/thumbnails/1.jpg)
1
Chapter 5
File Objects and Looping Statements
![Page 2: 1 Chapter 5 File Objects and Looping Statements. 2 Chapter 9 Topics l Using Data Files for I/O l While Statement Syntax l Count-Controlled Loops l Event-Controlled](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649d5f5503460f94a3eea9/html5/thumbnails/2.jpg)
2
Chapter 9 Topics
Using Data Files for I/O While Statement Syntax Count-Controlled Loops Event-Controlled Loops Using the End-of-File Condition Using a While Statement for Summing and
Counting Nested While Loops Loop Testing and Debugging
![Page 3: 1 Chapter 5 File Objects and Looping Statements. 2 Chapter 9 Topics l Using Data Files for I/O l While Statement Syntax l Count-Controlled Loops l Event-Controlled](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649d5f5503460f94a3eea9/html5/thumbnails/3.jpg)
3
To Use File I/O, you must
Import package java.io.* Choose valid identifiers and types for
your file variables and declare them Instantiate a file object for each file
variable Use your file identifiers in your I/O
statements (using available methods such as readLine, print, println)
Close the files when through
![Page 4: 1 Chapter 5 File Objects and Looping Statements. 2 Chapter 9 Topics l Using Data Files for I/O l While Statement Syntax l Count-Controlled Loops l Event-Controlled](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649d5f5503460f94a3eea9/html5/thumbnails/4.jpg)
4
What does instantiating a file do?
Associates the Java identifier for your file with the physical (disk) name for the file
Places a file pointer at the very beginning of the file, pointing to the first character in it
If the output file does not exist on disk, an empty file with that name is created
If the output file already exists, it is erased
![Page 5: 1 Chapter 5 File Objects and Looping Statements. 2 Chapter 9 Topics l Using Data Files for I/O l While Statement Syntax l Count-Controlled Loops l Event-Controlled](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649d5f5503460f94a3eea9/html5/thumbnails/5.jpg)
5
Using files for I/O
your variable
(of type BufferedReader)
your variable
(of type PrintWriter)
disk file“myInfile.dat”
disk file“myOutfile.dat”
executingprogram
input data output data
import.java.io.*;
![Page 6: 1 Chapter 5 File Objects and Looping Statements. 2 Chapter 9 Topics l Using Data Files for I/O l While Statement Syntax l Count-Controlled Loops l Event-Controlled](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649d5f5503460f94a3eea9/html5/thumbnails/6.jpg)
6
readLine() method
readLine() method of BufferedReader class uses no parameters and returns an object of class String.
It reads a line of input from the file, including the end-of-line mark, discards the EOL mark, and returns the rest of the line
String line;
line = inFile.readLine();
![Page 7: 1 Chapter 5 File Objects and Looping Statements. 2 Chapter 9 Topics l Using Data Files for I/O l While Statement Syntax l Count-Controlled Loops l Event-Controlled](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649d5f5503460f94a3eea9/html5/thumbnails/7.jpg)
7
How do you read a number?
Read a string and convert it to a number
int numberOfDependents; line = inFile.readLine();
numberOfDependents = Integer.parseInt(line);
double taxRate;
line = inFile.readLine();
taxRate = Double.parseDouble(line);
![Page 8: 1 Chapter 5 File Objects and Looping Statements. 2 Chapter 9 Topics l Using Data Files for I/O l While Statement Syntax l Count-Controlled Loops l Event-Controlled](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649d5f5503460f94a3eea9/html5/thumbnails/8.jpg)
8
print() and println() methods
print() writes its parameters on the file to which the method is applied
println() is just like print, but it appends an end-of-line before returning.
Sound familiar?
print and println in class PrintWriter behave exactly the same as those methods of the same name we have used with System.out
![Page 9: 1 Chapter 5 File Objects and Looping Statements. 2 Chapter 9 Topics l Using Data Files for I/O l While Statement Syntax l Count-Controlled Loops l Event-Controlled](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649d5f5503460f94a3eea9/html5/thumbnails/9.jpg)
9
An exception is an unusual situation detected while a program is running; it halts the normal execution of the method
Java recognizes two types of exceptions, checked and unchecked
Unchecked exceptions can be ignored, but checked exceptions must be explicitly recognized by the program
I/O Methods and Exceptions
![Page 10: 1 Chapter 5 File Objects and Looping Statements. 2 Chapter 9 Topics l Using Data Files for I/O l While Statement Syntax l Count-Controlled Loops l Event-Controlled](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649d5f5503460f94a3eea9/html5/thumbnails/10.jpg)
10
Forwarding an exception
An exception can be forwarded by adding a throws clause to a method heading. The clause specifies the name of the exception that is being forwarded
public static void main(String[ ] args)
throws IOException
The exception is passed to the method’s caller, until an exception handler is found, or passing terminates with the JVM
![Page 11: 1 Chapter 5 File Objects and Looping Statements. 2 Chapter 9 Topics l Using Data Files for I/O l While Statement Syntax l Count-Controlled Loops l Event-Controlled](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649d5f5503460f94a3eea9/html5/thumbnails/11.jpg)
11
BufferedReader and FileWriter classes throw an exception called IOException.
PrintWriter does not throw any exceptions, but the PrintWriter constructor must be passed a FileWriter object that can throw an IOException
More about exceptions in Chapter 9
IOException
![Page 12: 1 Chapter 5 File Objects and Looping Statements. 2 Chapter 9 Topics l Using Data Files for I/O l While Statement Syntax l Count-Controlled Loops l Event-Controlled](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649d5f5503460f94a3eea9/html5/thumbnails/12.jpg)
12
A loop is a repetition control structure
It causes a single statement or block to be executed repeatedly while an expression is true
What is a loop?
![Page 13: 1 Chapter 5 File Objects and Looping Statements. 2 Chapter 9 Topics l Using Data Files for I/O l While Statement Syntax l Count-Controlled Loops l Event-Controlled](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649d5f5503460f94a3eea9/html5/thumbnails/13.jpg)
13
Two types of loops
count-controlled loops
repeat a specified number of times
event-controlled loopssomething happens inside the loop body that causes the repetition to stop
![Page 14: 1 Chapter 5 File Objects and Looping Statements. 2 Chapter 9 Topics l Using Data Files for I/O l While Statement Syntax l Count-Controlled Loops l Event-Controlled](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649d5f5503460f94a3eea9/html5/thumbnails/14.jpg)
14
While Statement
while ( Expression )
{
.
. // loop body
.
}
NOTE: Loop body can be a single statement, a null statement, or a block
![Page 15: 1 Chapter 5 File Objects and Looping Statements. 2 Chapter 9 Topics l Using Data Files for I/O l While Statement Syntax l Count-Controlled Loops l Event-Controlled](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649d5f5503460f94a3eea9/html5/thumbnails/15.jpg)
15
When the expression is tested and found to be false, the loop is exited and control passes to the statement that follows the loop body
WHILE LOOP
false
true
bodystatement
Expression
![Page 16: 1 Chapter 5 File Objects and Looping Statements. 2 Chapter 9 Topics l Using Data Files for I/O l While Statement Syntax l Count-Controlled Loops l Event-Controlled](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649d5f5503460f94a3eea9/html5/thumbnails/16.jpg)
16
An initialization of the loop control variable An expression to test for continuing the loop An update of the loop control variable to be executed within each iteration of the body
Count-controlled loops contain
![Page 17: 1 Chapter 5 File Objects and Looping Statements. 2 Chapter 9 Topics l Using Data Files for I/O l While Statement Syntax l Count-Controlled Loops l Event-Controlled](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649d5f5503460f94a3eea9/html5/thumbnails/17.jpg)
17
int loopCount; // Declare loop variable
loopCount = 1; // Initialize loop variable
while (loopCount <= 10) // Test expression
{
. // Repeated actions
.
.
loopCount = loopCount++; // Update loop variable
}
Count-controlled Pattern
![Page 18: 1 Chapter 5 File Objects and Looping Statements. 2 Chapter 9 Topics l Using Data Files for I/O l While Statement Syntax l Count-Controlled Loops l Event-Controlled](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649d5f5503460f94a3eea9/html5/thumbnails/18.jpg)
18
int count; // Declare loop variable
count = 1; // Initialize loop variable
while (count <= 4) // Test expression
{ // Repeated action
System.out.println(“count is “ + count); count ++; // Update loop variable
}
System.out.println(“Done”);
Count-controlled Example
![Page 19: 1 Chapter 5 File Objects and Looping Statements. 2 Chapter 9 Topics l Using Data Files for I/O l While Statement Syntax l Count-Controlled Loops l Event-Controlled](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649d5f5503460f94a3eea9/html5/thumbnails/19.jpg)
19
Count-controlled loopint count;
count = 1;
while ( count <= 4 ) {
System.out.println
( “count is “ + count );
count++;
}
System.out.println(“Done”);
OUTPUT
count
![Page 20: 1 Chapter 5 File Objects and Looping Statements. 2 Chapter 9 Topics l Using Data Files for I/O l While Statement Syntax l Count-Controlled Loops l Event-Controlled](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649d5f5503460f94a3eea9/html5/thumbnails/20.jpg)
20
int count;
count = 1;
while ( count <= 4 ) {
System.out.println
( “count is “ + count );
count++;
}
System.out.println( “Done” );
OUTPUT
count
1
![Page 21: 1 Chapter 5 File Objects and Looping Statements. 2 Chapter 9 Topics l Using Data Files for I/O l While Statement Syntax l Count-Controlled Loops l Event-Controlled](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649d5f5503460f94a3eea9/html5/thumbnails/21.jpg)
21
int count;
count = 1;
while ( count <= 4 ) { TRUE
System.out.println
( “count is “ + count );
count++;
}
System.out.println( “Done” );
count
1 OUTPUT
![Page 22: 1 Chapter 5 File Objects and Looping Statements. 2 Chapter 9 Topics l Using Data Files for I/O l While Statement Syntax l Count-Controlled Loops l Event-Controlled](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649d5f5503460f94a3eea9/html5/thumbnails/22.jpg)
22
int count;
count = 1;
while ( count <= 4 ) {
System.out.println
( “count is “ + count );
count++;
}
System.out.println( “Done” );
count
1
OUTPUT count is 1
![Page 23: 1 Chapter 5 File Objects and Looping Statements. 2 Chapter 9 Topics l Using Data Files for I/O l While Statement Syntax l Count-Controlled Loops l Event-Controlled](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649d5f5503460f94a3eea9/html5/thumbnails/23.jpg)
23
int count;
count = 1;
while ( count <= 4 ) {
System.out.println
( “count is “ + count );
count++;
}
System.out.println( “Done” );
count
2
OUTPUT count is 1
![Page 24: 1 Chapter 5 File Objects and Looping Statements. 2 Chapter 9 Topics l Using Data Files for I/O l While Statement Syntax l Count-Controlled Loops l Event-Controlled](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649d5f5503460f94a3eea9/html5/thumbnails/24.jpg)
24
int count;
count = 1;
while ( count <= 4 ) { TRUE
System.out.println
( “count is “ + count );
count++;
}
System.out.println( “Done” );
count
2
OUTPUT count is 1
![Page 25: 1 Chapter 5 File Objects and Looping Statements. 2 Chapter 9 Topics l Using Data Files for I/O l While Statement Syntax l Count-Controlled Loops l Event-Controlled](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649d5f5503460f94a3eea9/html5/thumbnails/25.jpg)
25
int count;
count = 1;
while ( count <= 4 ) {
System.out.println
( “count is “ + count );
count++;
}
System.out.println( “Done” );
OUTPUT count is 1 count is 2
count
2
![Page 26: 1 Chapter 5 File Objects and Looping Statements. 2 Chapter 9 Topics l Using Data Files for I/O l While Statement Syntax l Count-Controlled Loops l Event-Controlled](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649d5f5503460f94a3eea9/html5/thumbnails/26.jpg)
26
int count;
count = 1;
while ( count <= 4 ) {
System.out.println
( “count is “ + count );
count++;
}
System.out.println( “Done” );
count
3
OUTPUT count is 1 count is 2
![Page 27: 1 Chapter 5 File Objects and Looping Statements. 2 Chapter 9 Topics l Using Data Files for I/O l While Statement Syntax l Count-Controlled Loops l Event-Controlled](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649d5f5503460f94a3eea9/html5/thumbnails/27.jpg)
27
int count;
count = 1;
while ( count <= 4 ) { TRUE
System.out.println
( “count is “ + count );
count++;
}
System.out.println( “Done” );
count
3
OUTPUT count is 1 count is 2
![Page 28: 1 Chapter 5 File Objects and Looping Statements. 2 Chapter 9 Topics l Using Data Files for I/O l While Statement Syntax l Count-Controlled Loops l Event-Controlled](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649d5f5503460f94a3eea9/html5/thumbnails/28.jpg)
28
int count;
count = 1;
while ( count <= 4 ) {
System.out.println
( “count is “ + count );
count++;
}
System.out.println( “Done” );
OUTPUT count is 1 count is 2 count is 3
count
3
![Page 29: 1 Chapter 5 File Objects and Looping Statements. 2 Chapter 9 Topics l Using Data Files for I/O l While Statement Syntax l Count-Controlled Loops l Event-Controlled](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649d5f5503460f94a3eea9/html5/thumbnails/29.jpg)
29
int count;
count = 1;
while ( count <= 4 ) {
System.out.println
( “count is “ + count );
count++;
}
System.out.println( “Done” );
OUTPUT count is 1 count is 2 count is 3
count
4
![Page 30: 1 Chapter 5 File Objects and Looping Statements. 2 Chapter 9 Topics l Using Data Files for I/O l While Statement Syntax l Count-Controlled Loops l Event-Controlled](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649d5f5503460f94a3eea9/html5/thumbnails/30.jpg)
30
int count;
count = 1;
while ( count <= 4 ) { TRUE
System.out.println
( “count is “ + count );
count++;
}
System.out.println( “Done” );
count
4
OUTPUT count is 1 count is 2 count is 3
![Page 31: 1 Chapter 5 File Objects and Looping Statements. 2 Chapter 9 Topics l Using Data Files for I/O l While Statement Syntax l Count-Controlled Loops l Event-Controlled](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649d5f5503460f94a3eea9/html5/thumbnails/31.jpg)
31
int count;
count = 1;
while ( count <= 4 ) {
System.out.println
( “count is “ + count );
count++;
}
System.out.println( “Done” );
OUTPUT count is 1 count is 2 count is 3
count is 4
count
4
![Page 32: 1 Chapter 5 File Objects and Looping Statements. 2 Chapter 9 Topics l Using Data Files for I/O l While Statement Syntax l Count-Controlled Loops l Event-Controlled](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649d5f5503460f94a3eea9/html5/thumbnails/32.jpg)
32
int count;
count = 1;
while ( count <= 4 ) {
System.out.println
( “count is “ + count );
count++;
}
System.out.println( “Done” );
OUTPUT count is 1 count is 2 count is 3
count is 4
count
5
![Page 33: 1 Chapter 5 File Objects and Looping Statements. 2 Chapter 9 Topics l Using Data Files for I/O l While Statement Syntax l Count-Controlled Loops l Event-Controlled](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649d5f5503460f94a3eea9/html5/thumbnails/33.jpg)
33
int count;
count = 1;
while ( count <= 4 ) { FALSE
System.out.println
( “count is “ + count );
count++;
}
System.out.println( “Done” );
count
5
OUTPUT count is 1 count is 2 count is 3
count is 4
![Page 34: 1 Chapter 5 File Objects and Looping Statements. 2 Chapter 9 Topics l Using Data Files for I/O l While Statement Syntax l Count-Controlled Loops l Event-Controlled](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649d5f5503460f94a3eea9/html5/thumbnails/34.jpg)
34
int count;
count = 1;
while ( count <= 4 ) {
System.out.println
( “count is “ + count );
count++;
}
System.out.println( “Done” );
count
5
OUTPUT count is 1 count is 2 count is 3
count is 4 Done
![Page 35: 1 Chapter 5 File Objects and Looping Statements. 2 Chapter 9 Topics l Using Data Files for I/O l While Statement Syntax l Count-Controlled Loops l Event-Controlled](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649d5f5503460f94a3eea9/html5/thumbnails/35.jpg)
35
• dataFile contains 100 blood pressures, one to a line
• Use a while loop to read the 100 blood pressures and find their total
Count-Controlled Loop Example
![Page 36: 1 Chapter 5 File Objects and Looping Statements. 2 Chapter 9 Topics l Using Data Files for I/O l While Statement Syntax l Count-Controlled Loops l Event-Controlled](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649d5f5503460f94a3eea9/html5/thumbnails/36.jpg)
36
// Count-controlled loop
int thisBP; int total; int count; count = 1; // Initializetotal = 0;
while (count <= 100) // Test expression{ thisBP = Integer.parseInt(dataFile.readLine()); total = total + thisBP; count++; // Update}
System.out.println(“The total = “ + total);
![Page 37: 1 Chapter 5 File Objects and Looping Statements. 2 Chapter 9 Topics l Using Data Files for I/O l While Statement Syntax l Count-Controlled Loops l Event-Controlled](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649d5f5503460f94a3eea9/html5/thumbnails/37.jpg)
37
Event-controlled loops
Sentinel controlled Keep processing data until a special value which is not a possible data value is entered to indicate that processing should stop
End-of-file controlled Keep processing data as long as there is more data in the file
Flag controlled Keep processing data until the value of a flag changes in the loop body
![Page 38: 1 Chapter 5 File Objects and Looping Statements. 2 Chapter 9 Topics l Using Data Files for I/O l While Statement Syntax l Count-Controlled Loops l Event-Controlled](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649d5f5503460f94a3eea9/html5/thumbnails/38.jpg)
38
Examples of kinds of loops
Count controlled loop Read exactly 100 blood pressures from a file.
End-of-file controlledloop
Read all the blood pressures from a file no matter how many are there.
Flag controlledloop
Read blood pressures until a dangerously high BP (200 or more) is read.
![Page 39: 1 Chapter 5 File Objects and Looping Statements. 2 Chapter 9 Topics l Using Data Files for I/O l While Statement Syntax l Count-Controlled Loops l Event-Controlled](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649d5f5503460f94a3eea9/html5/thumbnails/39.jpg)
39
A sentinel-controlled loop
Requires a “priming read”
“Priming read” means you read one data value (or set of data values) before entering the while loop
Process data value(s) and then read next value(s) at end of loop
![Page 40: 1 Chapter 5 File Objects and Looping Statements. 2 Chapter 9 Topics l Using Data Files for I/O l While Statement Syntax l Count-Controlled Loops l Event-Controlled](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649d5f5503460f94a3eea9/html5/thumbnails/40.jpg)
40
// Sentinel is negative blood pressure.int thisBP; int total; int count; count = 1; // Initializetotal = 0; // Priming readthisBP = Integer.parseInt(dataFile.readLine());while (thisBP > 0) // Test expression{ total = total + thisBP; count++; // Update thisBP = Integer.parseInt(dataFile.readLine());}
System.out.println(“The total = “ + total);
![Page 41: 1 Chapter 5 File Objects and Looping Statements. 2 Chapter 9 Topics l Using Data Files for I/O l While Statement Syntax l Count-Controlled Loops l Event-Controlled](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649d5f5503460f94a3eea9/html5/thumbnails/41.jpg)
41
An end-of-file controlled loop
depends on fact that readLine
returns null if there is no more data
![Page 42: 1 Chapter 5 File Objects and Looping Statements. 2 Chapter 9 Topics l Using Data Files for I/O l While Statement Syntax l Count-Controlled Loops l Event-Controlled](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649d5f5503460f94a3eea9/html5/thumbnails/42.jpg)
42
// Read and sum until end of lineint thisBP; int total; int count; count = 1; // Initializetotal = 0; String line;line = dataFile.readLine();while (line != null) // Test expression{ thisBP = Integer.parseInt(line); total = total + thisBP; count++; // Update line = dataFile.readLine();}System.out.println(“The total = “ + total);
![Page 43: 1 Chapter 5 File Objects and Looping Statements. 2 Chapter 9 Topics l Using Data Files for I/O l While Statement Syntax l Count-Controlled Loops l Event-Controlled](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649d5f5503460f94a3eea9/html5/thumbnails/43.jpg)
43
Flag-controlled loops
Use meaningful name for the flag Initialize flag (to true or false) Test the flag in the loop test
expression Change the value of the flag in loop
body when the appropriate condition occurs
![Page 44: 1 Chapter 5 File Objects and Looping Statements. 2 Chapter 9 Topics l Using Data Files for I/O l While Statement Syntax l Count-Controlled Loops l Event-Controlled](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649d5f5503460f94a3eea9/html5/thumbnails/44.jpg)
44
A flag-controlled loop
Count and sum the first 10 odd numbers in a data file
Initialize flag notDone to true
Use while(notDone) for loop test Change flag to false when 10 odd
numbers have been read or if EOF is reached first
![Page 45: 1 Chapter 5 File Objects and Looping Statements. 2 Chapter 9 Topics l Using Data Files for I/O l While Statement Syntax l Count-Controlled Loops l Event-Controlled](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649d5f5503460f94a3eea9/html5/thumbnails/45.jpg)
45
count = 0;
sum = 0;
notDone = true;
while ( notDone ) {
line = dataFile.readLine( ); // Get a line
if (line != null) // Got a line? { number = Integer.parseInt(line);
if (number % 2 == 1) // Is number odd? {
count++;
sum = sum + number;
notDone = ( count < 10 ); } } else // Reached EOF unexpectedly { errorFile.println(“EOF reached before ten odd values.”) notDone = false; // Change flag value } }
![Page 46: 1 Chapter 5 File Objects and Looping Statements. 2 Chapter 9 Topics l Using Data Files for I/O l While Statement Syntax l Count-Controlled Loops l Event-Controlled](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649d5f5503460f94a3eea9/html5/thumbnails/46.jpg)
46
Loops often used to
Count all data values
Count special data values
Sum data values
![Page 47: 1 Chapter 5 File Objects and Looping Statements. 2 Chapter 9 Topics l Using Data Files for I/O l While Statement Syntax l Count-Controlled Loops l Event-Controlled](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649d5f5503460f94a3eea9/html5/thumbnails/47.jpg)
47
// Flag and EOF controlled loop
countGoodReadings = 0; // Initialize
isSafe = true;
while (isSafe && (line = dataFile.readLine() != null)) {
thisBP = Integer.valueOf(line).intValue();
if (thisBP >= 200)isSafe = false; // Change flag
else countGoodReadings++; // Update
}
System.out.println(“There were ” + countGoodReadings + “ safe blood pressure readings.”);
![Page 48: 1 Chapter 5 File Objects and Looping Statements. 2 Chapter 9 Topics l Using Data Files for I/O l While Statement Syntax l Count-Controlled Loops l Event-Controlled](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649d5f5503460f94a3eea9/html5/thumbnails/48.jpg)
48
initialize outer loop
while ( outer loop condition )
{ . . .
initialize inner loop
while ( inner loop condition )
{
inner loop processing and update
}
. . .}
Pattern of a nested loop
![Page 49: 1 Chapter 5 File Objects and Looping Statements. 2 Chapter 9 Topics l Using Data Files for I/O l While Statement Syntax l Count-Controlled Loops l Event-Controlled](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649d5f5503460f94a3eea9/html5/thumbnails/49.jpg)
49
Nested loop design
Begin with outer loop
When you get to where the inner loop appears, make it a separate module and come back to its design later
![Page 50: 1 Chapter 5 File Objects and Looping Statements. 2 Chapter 9 Topics l Using Data Files for I/O l While Statement Syntax l Count-Controlled Loops l Event-Controlled](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649d5f5503460f94a3eea9/html5/thumbnails/50.jpg)
50
Algorithm uses Nested Loops
Get a data line from the data file While more data
obtain starCount from the data line use a count-controlled loop to print
starCount asterisks to the output file print a newline character to the
output file read next data line from the data file
Print “End” to the output file
![Page 51: 1 Chapter 5 File Objects and Looping Statements. 2 Chapter 9 Topics l Using Data Files for I/O l While Statement Syntax l Count-Controlled Loops l Event-Controlled](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649d5f5503460f94a3eea9/html5/thumbnails/51.jpg)
51
// Using nested loopsline = dataFile.readLine(); // Get data linewhile (line != null) // While more data{ starCount = Integer.parseInt(line); loopCount = 1; // Loop to print
asterisks while (loopCount <= starCount) { outFile.print(‘*’); loopCount ++; } outFile.println(); line = dataFile.readLine(); // Get next data line} outFile.println(“End”);
![Page 52: 1 Chapter 5 File Objects and Looping Statements. 2 Chapter 9 Topics l Using Data Files for I/O l While Statement Syntax l Count-Controlled Loops l Event-Controlled](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649d5f5503460f94a3eea9/html5/thumbnails/52.jpg)
52
Information about 20 books in file
bookfile.dat
P 3.98H 7.41P 8.79
.
.
.
Price of book
Hardback orPaperback?
Write a program to find total value of all books
![Page 53: 1 Chapter 5 File Objects and Looping Statements. 2 Chapter 9 Topics l Using Data Files for I/O l While Statement Syntax l Count-Controlled Loops l Event-Controlled](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649d5f5503460f94a3eea9/html5/thumbnails/53.jpg)
53
String line; // Declarations String kind; double price; double total = 0.0; int count = 1;
while (count <= 20) { line = dataFile.readLine() kind = line.substring(0, 1); price = Double.parseDouble( line.substring(1, line.length())); total = total + price; count ++; }
![Page 54: 1 Chapter 5 File Objects and Looping Statements. 2 Chapter 9 Topics l Using Data Files for I/O l While Statement Syntax l Count-Controlled Loops l Event-Controlled](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649d5f5503460f94a3eea9/html5/thumbnails/54.jpg)
54
Trace of Program Variables
count kind price total 0.0
1 “P” 3.98 3.98
2 “H” 7.41 11.39
3 “P” 8.79 20.18
4 etc.
20
21 so loop terminates
![Page 55: 1 Chapter 5 File Objects and Looping Statements. 2 Chapter 9 Topics l Using Data Files for I/O l While Statement Syntax l Count-Controlled Loops l Event-Controlled](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649d5f5503460f94a3eea9/html5/thumbnails/55.jpg)
55
Accessing characters in a line
readLine reads a line of text from a file Lines are converted to numbers using
methods in numeric classes How do you access the characters in a line?
line.charAt(index) returns the character at the index position in the line
line.charAt(0) is the first character; line.charAt(line.length()-1) is the last character
![Page 56: 1 Chapter 5 File Objects and Looping Statements. 2 Chapter 9 Topics l Using Data Files for I/O l While Statement Syntax l Count-Controlled Loops l Event-Controlled](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649d5f5503460f94a3eea9/html5/thumbnails/56.jpg)
56
// Counts number of commas on each line of datalineCount = 0; inLine = inFile.readLine(); // Priming readwhile (inLine != null) // Outer loop test for EOF{ lineCount++; commaCount= 0; index = 0; // Initialize loop count while (index < inLine.length()) { if (inLine.charAt(index) == ',') commaCount++; index++; } System.out.println("Found " + commaCount + " commas on line " + lineCount); inLine = inFile.readLine(); //Update outer exit condition}System.out.println("There are " + lineCount + " lines in the file.");
![Page 57: 1 Chapter 5 File Objects and Looping Statements. 2 Chapter 9 Topics l Using Data Files for I/O l While Statement Syntax l Count-Controlled Loops l Event-Controlled](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649d5f5503460f94a3eea9/html5/thumbnails/57.jpg)
57
Loop Definitions
Iteration counter A counter variable that is incremented in each iteration of a loop
Event counter A variable that is incremented each time a particular event occurs
![Page 58: 1 Chapter 5 File Objects and Looping Statements. 2 Chapter 9 Topics l Using Data Files for I/O l While Statement Syntax l Count-Controlled Loops l Event-Controlled](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649d5f5503460f94a3eea9/html5/thumbnails/58.jpg)
58
Object State The object’s state is the current set of values
that it contains
An object is instantiated with an initial state. If any of its methods can subsequently change its state, the object is said to be mutable
Methods that change an object’s state are called transformers
![Page 59: 1 Chapter 5 File Objects and Looping Statements. 2 Chapter 9 Topics l Using Data Files for I/O l While Statement Syntax l Count-Controlled Loops l Event-Controlled](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649d5f5503460f94a3eea9/html5/thumbnails/59.jpg)
59
Class TrafficLight
TrafficLight
knowCurrentColor
knowTimeRemaining .
. .
changeColor
Private data:
currentColor
timeRemaining
decrementTimeRemaining
![Page 60: 1 Chapter 5 File Objects and Looping Statements. 2 Chapter 9 Topics l Using Data Files for I/O l While Statement Syntax l Count-Controlled Loops l Event-Controlled](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649d5f5503460f94a3eea9/html5/thumbnails/60.jpg)
60
Loop Testing and Debugging Test data should test all sections of the program
Beware of infinite loops -- the program doesn’t stop
Check loop termination condition, and watch for an OBOB (off-by-1 bug)
Use algorithm walk-through to verify that appropriate conditions occur in the right places
Trace execution of loop by hand with code walk-through
Use a debugger (if available) to run program in “slow motion” or use debug output statements