How to Tell a Scanner Which Text File to Read From
ITEC 120
Reading data from a file
Objectives
Later successfully completing this lab yous will apply the Scanner class to read data from a file and yous will gain experience with iterators.
Assignment
Download: FileIO.java, first.txt, scores.txt
Reading lines from a file: start.txt
Scanner
You already know how to apply the Scanner class to read input from the keyboard. The Scanner class tin read from several different data sources including Strings and Files.
Yous will use a Scanner to browse lines from a file using two methods: hasNext and nextLine. The nextLine method returns the adjacent line from the file, equally a Cord. The hasNext method tells yous if there is some other line in the file to read. If you lot endeavour to read a line from a file that isn't there, y'all'll get a runtime exception. So y'all'll always cheque to see if there is a line to read before you read information technology:
while the file has a side by side line, read it (as a String)
When you reach the end of the file, the hasNext method returns false.
Get started
Start with something uncomplicated: Read and print lines from the start.txt file.
Take a wait at FileIO.coffee:
Notice the highlighted lines of code. When working with files, it'due south possible to run into problems such equally trying to open a file that isn't in that location or trying to read data past the end of a file. These bug throw exceptions (runtime errors). Because of this, we take to state in our code that these exceptions are a possibility. We'll discuss Exceptions and exception handling in greater detail in two weeks, only for at present, empathise that if you are going to read from a file using the Scanner course, yous must import java.io.*, and state that the method doing the file reading could throw an IOException.
Add lawmaking to your FileIO.coffee to read and print each line from the beginning.txt file. Kickoff, you'll demand to create a Scanner to scan a File instead of the keyboard. When nosotros read input from the keyboard, we used:
Scanner scan = new Scanner(System.in);
System.in is the keyboard, and we passed it to the Scanner constructor. To read a file, you must pass a file object to the Scanner constructor instead. (Hey, I wonder if the Scanner grade has multiple constructors?)
File is a java class (in java.io), and to create a file nosotros could:
File fileObj = new File("filename.filetype");
In the main method, create a File object for the start.txt file, and pass that to a Scanner. Write a loop to read and print all the lines in the first.txt file.
Processing data in the scores.txt file
Processing Data
Thus far nosotros accept read and printed rows of data from the start.txt file. Add code to your file to read and print each line of the scores.txt file.
Usually, nosotros desire to do more than with the data stored in a file. Often, nosotros need to parse the information independent on a line - break it into smaller pieces and clarify each slice.
Take a look at a row of data from the scores.txt file:
85,70,95,82,75
The data above represents 5 quiz scores. The information 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. CSV is a mutual import and export format for spreadsheets and databases.
You lot have read the row of data as a Cord, and at present you must extract each score from the string and stand for the score as an integer.
while the file has a next line, read information technology (as a Cord) while there is information on the line read and procedure each piece of data
We can employ the Scanner to extract numbers from a string. To do this, create a new Scanner (non the one you used to read lines from the file) and pass the entire line of information, as a String, to the Scanner's constructor. The Scanner has a default delimiter: whitespace. But a CSV file is comma-delimited.
Use the Scanner's useDelimiter method to gear up the delimiter to a comma (",").
Use the Scanner methods hasNext and nextInt to read all the integers from the line of data.
Print the loftier score, the low score, and the average score for each row of data.
Annotation: No arrays are needed to practise this! Process the data as you lot go.
Example output:
line: 85,70,95,82,75 // the whole line printed
scores: 85 70 95 82 75 // each score printed
Add together additional code to your loop to salve the highest and lowest score and print them on a third line:
line: 85,lxx,95,82,75
scores: 85 70 95 82 75
high: 95 low: 70
Add together additional code to your loop to sum the scores and print the sum on the 3rd line.
line: 85,70,95,82,75
scores: 85 lxx 95 82 75
high: 95 low: seventy sum: 407
Finally, replace the sum with the average.
line: 85,70,95,82,75
scores: 85 lxx 95 82 75
high: 95 low: seventy average: 81.four
You've probably noticed that there are a dissimilar 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 side by side methods). Add a few lines of quiz scores to your scores.txt file and run your programme again.
Submit Your Assignment
Submit FileIO.java to the Lab Submission Binder on D2L.
cangelosibeills43.blogspot.com
Source: https://sites.radford.edu/~itec120/2019spring-asbrennem/labs/L11b/description.html
Publicar un comentario for "How to Tell a Scanner Which Text File to Read From"