[an error occurred while processing this directive] CSE 142 (190) Lab 6

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:

Recall: Scanner Methods

To use these methods, you need a variable of type Scanner:

Scanner fileScanner = new Scanner(new File("input.txt"));
String theNextString = fileScanner.next();
Method name Outcome
next() reads and returns the next token as a String
nextLine() reads and returns as a String all the characters up to (but excluding) 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 whether there is still a token in the Scanner
hasNextLine() returns whether there is still at least one line left to be read in the Scanner
hasNextInt() returns whether the next token can be read as an int
hasNextDouble() returns whether the next token can be read as an double

Exercise : Token-izing

How many tokens are in the following String? 3

welcome...to the matrix.

What are the tokens that the String breaks up into?

Exercise : More tokenizing

How many tokens are in the following String? 9

in fourteen-hundred 92\ncolumbus sailed the ocean blue :)

What are the tokens that the String breaks up into?

Exercise :

For the next several questions, consider 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 when it is run on the readme.txt file?

Scanner input = new Scanner(new File("readme.txt"));
int count = 0;
while (input.hasNextLine()) {
    System.out.println("input: " + input.nextLine());
    count++;
}
System.out.println(count + " total");

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 to solve this problem in Practice-it!

Exercise : flipLines practice-it

Write a static method named flipLines that accepts as its parameter a Scanner for an input file and that writes to the console the same file's contents with successive pairs of lines reversed in order. For example, if the input file contains the following text:

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

"Beware the Jabberwock, my son,
the jaws that bite, the claws that catch,
Beware the JubJub bird and shun
the frumious bandersnatch."

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,
"Beware the Jabberwock, my son,

Beware the JubJub bird and shun
the jaws that bite, the claws that catch,
the frumious bandersnatch."

Click on the check-mark above to try to solve this problem in Practice-it!

Exercise : coinFlip practice-it

Write a static method named coinFlip that accepts as its parameter a Scanner for an input file. Assume that the input file data represents results of sets of coin flips that are either heads (H) or tails (T). Your method should consider each line to be a separate set of coin flips and should output to the console the number of heads and the percentage of heads in that line. If this percentage is more than 50%, you should print a "You win" message. For example, consider the following input 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!

Click on the check-mark above to try to solve this problem in Practice-it!

[an error occurred while processing this directive]