ITEC 120
L11b: Reading data from a file

Objectives

After successfully completing this lab you will use the Scanner class to read data from a file and you will gain experience with iterators.

Assignment

Download: FileReader.java, Lab11bTest.java, start.txt, scores.csv

The FileReader class abstracts the details of working with data files. Review the code in the FileReader class. You should be able to follow the code and explain how the methods work.

The FileReader class provides a constructor and two methods: hasNext and nextLine. Similar to the Scanner class, the FileReader constructor takes the name of the file to be read. The hasNext method is a predicate method that answers if the file has more data to be consumed. When all of the data in the file has been read the hasNext method returns false.

The nextLine method returns one row of data from a file. Each row is returned as a string and you will use a Scanner to parse the data. You will start by reading and writing rows (records) from a file. You will progress to reading and processing a list of numbers.

Scanner

You already know how to use the Scanner class to read input from the keyboard.  The Scanner class can read from several different data sources including strings and files.  The FileReader class uses a Scanner to read data from a file. In this lab, you will also use a Scanner to parse information in a String.

Reading Data

Warm up with something simple to ensure you understand how the File­Reader class works. Look through the code in the FileReader class and review the data in the start.txt file. The txt file extension indicates that the file contains text.

In the main method of the L11bTest driver, call the FileReader class and pass the string "start.txt" to open the start.txt data file. The nextLine method in the File­Reader class returns one row of data at a time until the end of the file is reached. Write a loop that reads and prints each row in the start.txt file until the hasNext method indicates the end of the file has been reached. Refer to listing 5.10 on page 242 of the textbook for an example of using the hasNext method.

Processing Data

Thus far we have read and printed rows of data from the start.txt file. Now we want to read data from the scores.csv file, and we want to process that data. Consider the row of data below.

85,70,95,82,75

The data above represents five quiz scores. The data is shown in comma separated values (CSV) format. The comma is the delimiter that separates the scores. Note that there are no spaces in the data, just commas and numbers.

Let's print the high score, the low score, and the average score for each row of data. To do this we must extract each score from the string and represent the score as an integer.

We can use the Scanner to extract numbers from a string. However, the default delimiter is a space. We must use the Scanner's useDelimiter method to set the delimiter to a comma (,).

In the main method, change the file name from "start.txt" to "scores.csv".

Create a Scanner to process the string returned by the nextLine method and call the useDelimiter method to set the delimiter to a comma. Create a nested loop that processes each row in the scores.csv file.

Printing the entire record (line from the file) and then print each score on one line separate by a space. You may assume that each row in the file contains at least one score (i.e., there are no empty rows in the data file). See the example output:

line: 85,70,95,82,75
scores: 85 70 95 82 75

Add additional code to your loop to save the highest and lowest score and print them on a third line:

line: 85,70,95,82,75
scores: 85 70 95 82 75
high: 95  low: 70

Add additional code to your loop to sum the scores and print the sum on the third line.

line: 85,70,95,82,75
scores: 85 70 95 82 75
high: 95  low: 70  sum: 407

Finally, replace the sum with the average.

line: 85,70,95,82,75
scores: 85 70 95 82 75
high: 95  low: 70  average: 81.4

You've probably noticed that there are a different number of quiz scores on each line. Your program should work regardless of how many quiz scores are on a line, and regardless of how many lines are in the file. This is what an iterator is good for (with the hasNext and next methods). Add a few lines of quiz scores to your scores.csv file and run your program again.

Submit Your Assignment

Submit your Lab11bTest.java file to the Lab11b dropbox on D2L.