Java Code to Read Comma Separated Text File Using Scanner
ITEC 120
Reading data from a file
Objectives
Afterward successfully completing this lab you volition use the Scanner grade to read information from a file and you will proceeds experience with iterators.
Assignment
Download: FileIO.java, offset.txt, scores.txt
Reading lines from a file: start.txt
Scanner
You already know how to use the Scanner class to read input from the keyboard. The Scanner grade can read from several dissimilar data sources including Strings and Files.
You will use a Scanner to scan lines from a file using two methods: hasNext and nextLine. The nextLine method returns the next line from the file, as a String. The hasNext method tells you if there is another line in the file to read. If you lot try to read a line from a file that isn't there, you'll go a runtime exception. So you'll e'er check to see if at that place is a line to read before y'all read it:
while the file has a next line, read it (equally a Cord)
When you reach the end of the file, the hasNext method returns simulated.
Get started
Showtime with something elementary: Read and print lines from the start.txt file.
Take a expect at FileIO.java:
Discover the highlighted lines of lawmaking. When working with files, it's possible to encounter problems such as trying to open a file that isn't in that location or trying to read data by the end of a file. These bug throw exceptions (runtime errors). Because of this, we have to state in our lawmaking that these exceptions are a possibility. Nosotros'll discuss Exceptions and exception handling in greater detail in two weeks, but for now, empathize that if you are going to read from a file using the Scanner class, y'all must import java.io.*, and state that the method doing the file reading could throw an IOException.
Add code to your FileIO.java to read and print each line from the get-go.txt file. First, you'll demand to create a Scanner to scan a File instead of the keyboard. When we read input from the keyboard, we used:
Scanner scan = new Scanner(Arrangement.in);
Organization.in is the keyboard, and we passed information technology to the Scanner constructor. To read a file, you must laissez passer a file object to the Scanner constructor instead. (Hey, I wonder if the Scanner class has multiple constructors?)
File is a java class (in java.io), and to create a file we could:
File fileObj = new File("filename.filetype");
In the principal method, create a File object for the start.txt file, and laissez passer that to a Scanner. Write a loop to read and print all the lines in the start.txt file.
Processing information in the scores.txt file
Processing Data
Thus far we have read and printed rows of information from the start.txt file. Add lawmaking to your file to read and print each line of the scores.txt file.
Usually, we desire to do more with the information stored in a file. Oftentimes, we demand to parse the data contained on a line - interruption it into smaller pieces and clarify each piece.
Take a wait at a row of data from the scores.txt file:
85,70,95,82,75
The data to a higher place represents five quiz scores. The information is shown in comma separated values (CSV) format. The comma is the delimiter that separates the scores. Notation that there are no spaces in the information, just commas and numbers. CSV is a common import and consign format for spreadsheets and databases.
Y'all take read the row of information as a Cord, and now you must extract each score from the string and represent the score equally an integer.
while the file has a next line, read it (as a String) while there is data on the line read and process each slice of data
We tin can use the Scanner to extract numbers from a cord. To do this, create a new Scanner (not the ane y'all used to read lines from the file) and laissez passer the entire line of data, as a String, to the Scanner'south constructor. The Scanner has a default delimiter: whitespace. Merely a CSV file is comma-delimited.
Use the Scanner'due south 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 high score, the low score, and the boilerplate score for each row of data.
NOTE: No arrays are needed to practise this! Process the information as you go.
Example output:
line: 85,70,95,82,75 // the whole line printed
scores: 85 seventy 95 82 75 // each score printed
Add additional code to your loop to save the highest and everyman 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 tertiary line.
line: 85,seventy,95,82,75
scores: 85 70 95 82 75
high: 95 low: 70 sum: 407
Finally, replace the sum with the average.
line: 85,lxx,95,82,75
scores: 85 lxx 95 82 75
loftier: 95 low: lxx average: 81.four
Y'all've probably noticed that in that location are a dissimilar number of quiz scores on each line. Your programme 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 skilful for (with the hasNext and next methods). Add a few lines of quiz scores to your scores.txt file and run your program once again.
Submit Your Assignment
Submit FileIO.java to the Lab Submission Folder on D2L.
Source: https://sites.radford.edu/~itec120/2019spring-asbrennem/labs/L11b/description.html
0 Response to "Java Code to Read Comma Separated Text File Using Scanner"
Post a Comment