Objectives

In this lab you will read data from a file using File objects, Scanners, and scanner lookahead.

Preparation

Sometimes people put the people.txt file in the wrong spot. If you chose not to have a separate src directory (as we prefer), your project should look like this:


If however you do have have a src directory, your project should look like this:

Pay attention to the indenting, the file people.txt file should not be inside the src folder. If it is, when you run the program it won't find your file.

Exercise 1: (2 points) Reading a file by tokens

When working with files, it is usually convenient to start with an existing program that works with files. The program CountWords.java counts the number of words in the file people.txt. Run it. It should count 50 words.

Close the editor tab for CountWords.java. Copy, paste, and rename it Ex1. Now open Ex1 for editing.

Modify the program by removing the counting stuff and changing it to print each token surrounded by square brackets [...]. Your output should look like:

[75] [Fresco,] [Al] [67] [Dwyer,] [Barb] [55] [Turner,] [Paige] [108] [Peace,] [Warren] ...

Paste your program and the output from the Console into your solutions document.

Exercise 2: (2 points) Checking token type

Close, copy, paste, and rename the program Ex2.

The file contains both integers and words. Modify the program to use hasNextInt() to test if the next token is an integer and if it is, read it as an integer with nextInt().  Otherwise use your previous code to read it as a word. Print the integers between parentheses (...). Your output should look like this:

(75) [Fresco,] [Al] (67) [Dwyer,] [Barb] (55) [Turner,] [Paige] (108) [Peace,] [Warren] ...

Paste your program and the output from the Console into your solutions document.

Exercise 3: (2 points) Reading by line

Close, copy, paste, and rename the program Ex3.

Modify the program to read the file line by line using nextLine(). Print each line between angle brackets <...>. Your output should look like this:

<75 Fresco, Al>
<67 Dwyer, Barb>
<55 Turner, Paige>
<108 Peace, Warren>
<46 Richman, Mary A.>
...

Paste your program and the output from the Console into your solutions document.

Exercise 4: (2 points) Reorganizing lines

Close, copy, paste, and rename the program Ex4.

Now you are going to modify the program to read the file and reorganize the order of each line to be [first-name last-name age]. This will be a little tricky because a few of the people also have a middle initial which we are going to treat as part of their first name. If we just processed the file by token as in exercise 2, this would cause some lines to have an extra token. Instead read the age with nextInt(), the last name with next(), and the rest of the line with nextLine(). Now print the 3 parts in the new order.

You will need to use substring() to strip the comma off the end of the last name. There is also an extra space on the start of the first name. You scan also strip this off with substring().

Your output should look like this:

Al Fresco 75
Barb Dwyer 67
Paige Turner 55
Warren Peace 108
Mary A. Richman 46
...

Paste your program and the output from the Console into your solutions document.

Exercise 5: (2 points) Finishing touches

Close, copy, paste, and rename the program Ex5.

Add a count and sum variable to your program. As you process each line, print the line number and add the age to the sum. At the end of the program calculate and print the average age.  Your output should look like this:

1. Al Fresco 75
2. Barb Dwyer 67
...
15. Ella Vader 25
16. Earl E. Bird 76

Average age is 50

Paste your program and the output from the Console into your solutions document.

YOU ARE DONE!

Save your solutions document and submit it.