University of Washington, CSE 142 (190)

Lab 6: File Processing

Except where otherwise noted, the contents of this document are Copyright 2010 Stuart Reges and Marty Stepp.

lab document created by Whitaker Brand and Marty Stepp

Today's lab

Goals for today:

Exercise : File Scanner declaration syntax

Which of the following choices is the correct syntax for declaring a Scanner to read the file example.txt in the current directory ?

Exercise -A: Tokenizing

How many tokens are in the following String? 3

welcome...to the matrix.

What are the tokens that the String breaks up into?

Exercise -B: More tokenizing

How many tokens are in the following String? 9

in fourteen-hundred 92
columbus sailed the ocean blue :)

What are the tokens that the String breaks up into?

Recall: Scanner Methods

Method name Description
next() reads and returns the next token as a String
nextLine() reads and returns as a String all the characters up to the next new line (\n)
nextInt() reads and returns the next token as an int, if possible
nextDouble() reads and returns the next token as double, if possible
hasNext() returns true if there is still a token in the Scanner
hasNextLine() returns true if there is still at least one line left to be read in the Scanner
hasNextInt() returns true if the next token can be read as an int
hasNextDouble() returns true if the next token can be read as an double

Exercise -A: Scanner practice

The next couple problems are about a file called readme.txt that has the following contents:

6.7  This file has
several input
LINES!

   10 20

What would be the output from the following code, as it would appear on the console?

Scanner input = new Scanner(new File("readme.txt"));
System.out.println(input.nextLine());  // 6.7  This file has
System.out.println(input.nextLine());  // several input
System.out.println(input.nextLine());  // LINES!

Exercise -B: Scanner practice

Input file: readme.txt

6.7  This file has
several input
LINES!

   10 20

What would be the output if the code was changed to the following?

Scanner input = new Scanner(new File("readme.txt"));
System.out.println(input.next());  // 6.7
System.out.println(input.next());  // This
System.out.println(input.next());  // file

Exercise -C: Scanner practice

Input file: readme.txt

6.7  This file has
several input
LINES!

   10 20

What would be the output for the following code? If there would be an error, write error .

Scanner input = new Scanner(new File("readme.txt"));
System.out.println(input.nextDouble());  // 6.7
System.out.println(input.nextDouble());  // error

Exercise -D: Scanner practice

Input file: readme.txt

6.7  This file has
several input
LINES!

   10 20

What would be the output for the following code? If there would be an error, write error .

Scanner input = new Scanner(new File("readme.txt"));
while (!input.hasNextInt()) {
    input.next();
}
System.out.println(input.nextInt());  // 10

Exercise : Words

Exercise - answer

import java.io.*;     // for File
import java.util.*;   // for Scanner

public class Words {
    public static void main(String[] args) throws FileNotFoundException {
        int wordCount = 0;
        Scanner input = new Scanner(new File("wordinput.txt"));
        
        // your code goes here ...
        while (input.hasNext()) {
            String word = input.next();
            wordCount++;
        }
        
        System.out.println("Total words = " + wordCount);
    }
}

Exercise : Getting textbook code files

We want to once again practice downloading textbook files to your computer by downloading and storing the case study from the end of chapter 6. To do so, follow these steps:

  1. Go to the class web page.
  2. Click on the "textbook" tab.
  3. Find the section labeled "Code Files" and click on the "code files" link.
  4. This will bring you to a directory listing that includes an entry for each chapter. Click on the link for "ch6".
  5. You want to download and save the files ZipLookup.java and zipcode.txt. Right-click on the file names and choose the option to save the link in whatever folder you have been using for lab work. Make sure to save them in the same folder.
  6. Compile and run ZipLookup.java in jGRASP. You might try using your own zip code with a relatively small radius like 0.5 miles to see what results you get. The program takes a little while to run because it has to search through a large data file twice.

Exercise : jGRASP Debugger

Exercise - jGRASP Debugger

continued on the next slide...

Exercise - jGRASP Debugger

Clear your previous break point and set a new break point inside on the printf inside the if. Then hit the resume button that looks like a play button and fill in the table below with the values for zip, lat2, and long2.

zip lat2 long2
20045 38.896599 -77.0319
20500 38.894781 -77.036122
20501 38.89872 -77.036198
20502 38.89872 -77.036198

Exercise : runningSum practice-it

Write a static method called runningSum that accepts as a parameter a Scanner holding a sequence of real numbers and that outputs the running sum of the numbers followed by the maximum running sum. For example if the Scanner contains the following data:

3.25 4.5 -8.25 7.25 3.5 4.25 -6.5 5.25

Your method should produce the following output:

running sum = 3.25 7.75 -0.5 6.75 10.25 14.5 8.0 13.25
max sum = 14.5

Click on the check-mark above to try out your solution in Practice-it!

Exercise : flipLines practice-it

Write a method named flipLines that accepts a Scanner for an input file and writes to the console the same file's contents with each pair of lines reversed in order. For example, if the file contains:

Twas brillig and the slithy toves
did gyre and gimble in the wabe.
All mimsey were the borogroves,
and the mome raths outgrabe.

The end

your method should produce the following output:

did gyre and gimble in the wabe.
Twas brillig and the slithy toves
and the mome raths outgrabe.
All mimsey were the borogroves,
The End

Exercise : countWords errors practice-it

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
// Counts the total lines and words in the given input scanner.
public static void countWords(Scanner input) {
    Scanner input = new Scanner(new File("example.txt"));
    int lineCount = 0;
    int wordCount = 0;
    
    while (input.nextLine()) {
        String line = input.line();       // read one line
        lineCount++;
        while (line.next()) {             // count tokens in line
            String word = line.hasNext;
            wordCount++;
        }
    }
}

The above attempted solution to Practice-It problem "countWords" has 5 errors. Open Practice-It from the link above, copy/paste this code into it, and fix the errors. Complete the code so that it passes the test cases.

Exercise - answer

  1. line 3: should not declare another Scanner for the file
  2. line 7: nextLine should be hasNextLine
  3. line 8: line should be nextLine
  4. line 10: need a second line Scanner to read the tokens of each line
  5. line 11: hasNext should be next()
  6. line 14: need to add 3 println statements to print line/word stats

Exercise - solution

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
// Counts the total lines and words in the given input scanner.
public static void countWords(Scanner input) {
    Scanner input = new Scanner(new File("example.txt"));
    int lineCount = 0;
    int wordCount = 0;
    
    while (input.hasNextLine()) {
        String line = input.nextLine();   // read one line
        lineCount++;
        Scanner lineScan = new Scanner(line);
        while (lineScan.hasNext()) {      // count tokens in line
            String word = line.next();
            wordCount++;
        }
    }
    
    System.out.println("Total lines = " + lineCount);
    System.out.println("Total words = " + wordCount);
    System.out.printf("Average words per line = %.3f\n", (double) wordCount / lineCount);
}

Exercise : coinFlip practice-it

Write a method named coinFlip that accepts a Scanner for an input file of coin flips that are heads (H) or tails (T). Consider each line to be a separate set of coin flips and output the number and percentage of heads in that line. If it is more than 50%, print "You win!". Consider the following file:

H T H H T
T t    t  T h  H
      h

For the input above, your method should produce the following output:

3 heads (60.0%)
You win!

2 heads (33.3%)

1 heads (100.0%)
You win!

If you finish them all...

If you finish all the exercises, try out our Practice-It web tool. It lets you solve Java problems from our Building Java Programs textbook.

You can view an exercise, type a solution, and submit it to see if you have solved it correctly.

Choose some problems from Chapter 6 and try to solve them!