using the while-statement to process data files. general procedure to access a data file general...

42
Using the while- statement to process data files

Upload: jason-atkinson

Post on 21-Jan-2016

214 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: 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

Using the while-statement to process data files

Page 2: 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

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

Page 3: 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

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

Page 4: 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

Previously discussed: reading from the keyboard

• Summary: reading a floating point number from the keyboard

Page 5: 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

Previously discussed: reading from the keyboard (cont.)

• The methods in the Scanner class can also be used to read input data from a file

Page 6: 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

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

Page 7: 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

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)

Page 8: 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

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

Page 9: 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

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

Page 10: 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

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"

Page 11: 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

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

Page 12: 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

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.

Page 13: 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

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

Page 14: 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

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

Page 15: 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

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

Page 16: 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

Checking for available input data (cont.)

• OK, we have covered everything we need to process data files in Java !

Page 17: 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

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.

Page 18: 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

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

Page 19: 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

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

Page 20: 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

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

Page 21: 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

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;

Page 22: 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

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

Page 23: 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

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

Page 24: 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

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/inp1

• 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

Page 25: 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

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 !

Page 26: 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

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

Page 27: 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

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.

Page 28: 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

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;

Page 29: 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

Programming example 2: sum the (floating point) data in a data file (cont.)

• Algorithm in Structure Diagram form:

Page 30: 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

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

Page 31: 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

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

Page 32: 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

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/inp2

• 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

Page 33: 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

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

Page 34: 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

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.

Page 35: 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

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

Page 36: 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

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

Page 37: 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

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

Page 38: 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

Programming example 3: compute the average of (floating point) data in a data file (cont.)

• Algorithm in Structure Diagram form:

Page 39: 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

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

Page 40: 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

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

Page 41: 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

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/inp2

• 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

Page 42: 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

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