In this lab you will read data from a file using File objects, Scanners, and scanner lookahead.
- Set up a new eclipse project for each lab (detailed instructions)
- Copy the files CountWords.java and people.txt into your project folder.
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.
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:
Paste your program and the output from the Console into your solutions document.
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:
Paste your program and the output from the Console into your solutions document.
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:
<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.
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:
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.
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:
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.
Save your solutions document and submit it.