Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 Using the while-statement to process data files

2 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

3 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

4 Previously discussed: reading from the keyboard Summary: reading a floating point number from the keyboard

5 Previously discussed: reading from the keyboard (cont.) The methods in the Scanner class can also be used to read input data from a file

6 Opening a data file How to open a file in Java: The variable myFile contains information about the opened file The variable myFile will be used in read operations File myFile; // Define a "File" type variable to receive // the opened file myFile = new File("Path-name-of-the-file"); // Open the file

7 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)

8 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

9 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

10 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"

11 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

12 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.

13 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.)

14 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.)

15 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.)

16 Checking for available input data (cont.) OK, we have covered everything we need to process data files in Java !

17 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.

18 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; }

19 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.)

20 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"); }

21 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;

22 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.*;

23 Programming example 1: print the content of a data file (cont.) File read errors in the java.io package are reported by exceptions A method that uses methods in the java.io package must contain the declaration Exceptions is a very advanced programming concept that is not discussed in this course.... throws IOExceptions

24 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

25 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 !

26 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

27 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.

28 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;

29 Programming example 2: sum the (floating point) data in a data file (cont.) Algorithm in Structure Diagram form:

30 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

31 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); }

32 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

33 Programming example 1: print the content of a data file (cont.) Example run: Input file inp2: Output of the program: 2.1 4.2 5.2 6.1 Sum = 17.6

34 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.

35 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

36 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

37 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; // Initial value ("clear the slate") N = 0 // # 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

38 Programming example 3: compute the average of (floating point) data in a data file (cont.) Algorithm in Structure Diagram form:

39 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

40 Programming example 3: compute the average of (floating point) data in a data file (cont.) sum = 0.0; // Initialize ("clear the slate") N = 0; // 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); }

41 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

42 Programming example 3: compute the average of (floating point) data in a data file (cont.) Example run: Input file inp3: Output of the program: 2.1 4.2 5.2 6.1 Average = 4.4


Download ppt "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."

Similar presentations


Ads by Google