Using the while-statement to process data files General procedure to access a data file • General procedure in computer programming to read data from a data file 1. Open the data file • You provide the name of the data file to an "open a file" library function that will open the data file (Different programming languages will provide a different "open a file" library function) • This "open a file" library function will return some information (let's call it X) back to you that you can use to access the opened file That information is used in the computer to identified the opened file General procedure to access a data file 2. With the information X, you can then use a "read something from a file" method to read data from the opened file 3. There are other helpful methods on an opened file. Some method let you check if you have reached the end of the file Previously discussed: reading from the keyboard • Summary: reading a floating point number from the keyboard Previously discussed: reading from the keyboard (cont.) • The methods in the Scanner class can also be used to read input data from a file Opening a data file • How to open a file in Java: File myFile; // Define a "File" type variable to receive // the opened file myFile = new File("Path-name-of-the-file"); // Open the file The variable myFile contains information about the opened file The variable myFile will be used in read operations Scanning an opened file • Reading data from an opened file is a very complex task • Fortunately: • An opened file behaves exactly like a keyboard (which is represented by the variable System.in in Java) Scanning an opened file (cont.) • We can use the method in the Scanner class to help us read and convert the data into the desired encoding Scanning an opened file (cont.) • Previously discussed: • To construct a Scanner object using the keyboard System.in: Scanner in; // Define a Scanner typed variable in = new Scanner(System.in); // Construct a Scanner that read // data from keyboard System.in Scanning an opened file (cont.) • Read from a data file: • Construct a Scanner object using an opened file: /* --------------------------------Open a data file --------------------------------- */ File myFile; // Define a "File" type variable to receive // the opened file myFile = new File("Path-name-of-the-file"); // Open the file /* -------------------------------------------------Construct a Scanner from the opened file "myFile" -------------------------------------------------- */ Scanner in; // Define a Scanner typed variable in = new Scanner(myFile); // Construct a Scanner that read // data from opened file"myFile" Scanning an opened file (cont.) • From this point onwards, you can use • in.nextDouble() to read a floating point number from the data file • in.nextInt() to read an integer number from the data file • in.next() to read a string (word) from the data file Checking for available input data • There are very useful methods available in the Scanner class to test if the input file is empty (exhausted) or not. Checking for available input data (cont.) • Check function for input availability on Scanner typed variable in: • in.hasNextDouble() • returns true if the Scanner object in contains at least one double typed value in the input. • returns false otherwise (no more double typed value in the input.) Checking for available input data (cont.) • in.hasNextInt() • returns true if the Scanner object in contains at least one int typed value in the input. • returns false otherwise (no more int typed value in the input.) Checking for available input data (cont.) • in.hasNext() • returns true if the Scanner object in contains at least one String typed value in the input. • returns false otherwise (no more String typed value in the input.) Checking for available input data (cont.) • OK, we have covered everything we need to process data files in Java ! Programming example 1: print the content of a data file • Let's start simple: • Write a Java program that read the data from the file inp1 and... • print each word read to the terminal. Programming example 1: print the content of a data file (cont.) • Rough algorithm: Open the file "inp1" Construct a Scanner object using the opened file as long as ( there is data in the Scanner object ) { read a word from the Scanner object; print the word; } Programming example 1: print the content of a data file (cont.) • Algorithm in Structure Diagram form: (I have used the initialization form to define the variables myFile and in for brevity.) Programming example 1: print the content of a data file (cont.) • Java program: import java.io.*; import java.util.Scanner; public class File01 { public static void main(String[] args) throws IOException { File myFile = new File("inp1"); // Open file "inp1" Scanner in = new Scanner(myFile); // Make Scanner obj with opened file String x; // Variable to receive a string while ( in.hasNext() ) { x = in.next(); // Read a string (word) System.out.println(x); // Print string read } System.out.println("Done"); } } Programming example 1: print the content of a data file (cont.) • Additional programming code needed: • The File class is contained inside the java.io package We need to use the import statement: import java.io.File; Programming example 1: print the content of a data file (cont.) • However, the methods in the File class need other methods in the java.io package That's why we import all class in the java.io package with the import statement import java.io.*; Programming example 1: print the content of a data file (cont.) • File read errors in the java.io package are reported by exceptions • Exceptions is a very advanced programming concept that is not discussed in this course • A method that uses methods in the java.io package must contain the declaration .... throws IOExceptions Programming example 1: print the content of a data file (cont.) • Example Program: (Demo above code) – Prog file: http://mathcs.emory.edu/~cheung/Courses/170/Syllabus/07/Progs/ File01.java – Input data file inp1: http://mathcs.emory.edu/~cheung/Courses/170/Syllabus/07/Progs/i np1 • How to run the program: • Right click on both links and save in a scratch directory • To compile: javac File01.java • To run: java File01 Programming example 1: print the content of a data file (cont.) • Example run: • Input file inp1: Hello Class This is your first input data file ! Programming example 1: print the content of a data file (cont.) • Output of the program: Hello Class This is your first input data file ! Done Programming example 2: sum the (floating point) data in a data file • Programming problem: • Given a data file inp2 that contains a (unspecified number) floating point numbers • Write a Java program that read the data from the file inp2 and... • print the sum of the numbers to the terminal. Programming example 2: sum the (floating point) data in a data file (cont.) • Rough algorithm: Open the file "inp2" Construct a Scanner object using the opened file sum = 0; // Initial value ("clear the slate") as long as ( there is a double datum in the Scanner object ) { Read a floating point number from the Scanner object; Add the number to the sum; } Print sum; Programming example 2: sum the (floating point) data in a data file (cont.) • Algorithm in Structure Diagram form: Programming example 2: sum the (floating point) data in a data file (cont.) • Java program: import java.io.*; import java.util.Scanner; public class File01 { public static void main(String[] args) throws IOException { File myFile = new File("inp2"); // Open file "inp2" Scanner in = new Scanner(myFile); // Make Scanner obj with opened file double x; // Variable to receive a floating point number double sum; // Running sum Programming example 2: sum the (floating point) data in a data file (cont.) sum = 0.0; // Initialize ("clear the slate") while ( in.hasNextDouble() ) { x = in.nextDouble(); // Read a floating point number sum = sum + x; // Add to the running sum } System.out.println("Sum = " + sum); } } Programming example 1: print the content of a data file (cont.) • Example Program: (Demo above code) – Prog file: http://mathcs.emory.edu/~cheung/Courses/170/Syllabus/07/Progs/ File02.java – Input data file inp1: http://mathcs.emory.edu/~cheung/Courses/170/Syllabus/07/Progs/i np2 • How to run the program: • Right click on both links and save in a scratch directory • To compile: javac File02.java • To run: java File02 Programming example 1: print the content of a data file (cont.) • Example run: • Input file inp2: 2.1 4.2 5.2 Output of the program: Sum = 17.6 6.1 Programming example 3: compute the average of (floating point) data in a data file • Programming problem: • Given a data file inp3 that contains a (unspecified number) floating point numbers • Write a Java program that read the data from the file inp3 and... • print the average of the numbers to the terminal. Programming example 3: compute the average of (floating point) data in a data file (cont.) • Computing the average: • The average of a series of numbers can be computed by maintaining the following information: • the running sum • the number of items that was added into the running sum Programming example 3: compute the average of (floating point) data in a data file (cont.) • Example: • Input: 2.1, 3.2, 2.2, 1.3 • Running sum = 10.8 • Number of items = 4 • Average = 10.8/4 = 2.7 Programming example 3: compute the average of (floating point) data in a data file (cont.) • Rough algorithm: Open the file "inp3" Construct a Scanner object using the opened file sum = 0; N=0 // Initial value ("clear the slate") // # items added into sum as long as ( there is a double datum in the Scanner object ) { Read a floating point number from the Scanner object; Add the number to the sum; N++; } Print sum/N; // print average Programming example 3: compute the average of (floating point) data in a data file (cont.) • Algorithm in Structure Diagram form: Programming example 3: compute the average of (floating point) data in a data file (cont.) • Java program: import java.io.*; import java.util.Scanner; public class File01 { public static void main(String[] args) throws IOException { File myFile = new File("inp2"); // Open file "inp2" Scanner in = new Scanner(myFile); // Make Scanner obj with opened file double x; // Variable to receive a floating point number double sum; // Running sum int N; // # items added **** New code Programming example 3: compute the average of (floating point) data in a data file (cont.) sum = 0.0; N = 0; // Initialize ("clear the slate") // No items added **** New code while ( in.hasNextDouble() ) { x = in.nextDouble(); // Read a floating point number sum = sum + x; // Add to the running sum N++; // One more item added **** New code } System.out.println("Average = " + sum/N); } } Programming example 3: compute the average of (floating point) data in a data file (cont.) • Example Program: (Demo above code) – Prog file: http://mathcs.emory.edu/~cheung/Courses/170/Syllabus/07/Progs/ File03.java – Input data file inp1: http://mathcs.emory.edu/~cheung/Courses/170/Syllabus/07/Progs/i np2 • How to run the program: • Right click on both links and save in a scratch directory • To compile: javac File03.java • To run: java File03 Programming example 3: compute the average of (floating point) data in a data file (cont.) • Example run: • Input file inp3: 2.1 4.2 5.2 6.1 Output of the program: Average = 4.4