CS170 Lab 7 Data Files and Loops

advertisement
CS170
Lab 7
Data Files and Loops
Objectives of this lab:
•
•
•
use Scanner and File objects to read input data from files
understand the way a Scanner breaks input into tokens
explore token-based and line-based input
Exercise Preparation:
• Read about using Scanners with File objects before coming to lab:
◦ http://www.mathcs.emory.edu/~cheung/Courses/170/Syllabus/07/file-proc.html
•
Read/Skim the Java API documentation for the methods in the Scanner class which begin with
hasNext or next. (For example, nextLine(), hasNextLine(), nextInt(),
hasNextInt(), etc.)
◦ http://docs.oracle.com/javase/8/docs/api/java/util/Scanner.html
•
These webpages tell you everything you need for this lab. Lab will be MUCH easier if you
have read this material before the lab session.
•
Start a terminal application and prepare your lab7 directory:
◦ mkdir ~/cs170/lab7
◦ cd ~/cs170/lab7
◦ copy/save the files poe.txt and Words.java from the class website into your lab7
directory.
•
•
If you examine the poe.txt file by typing
more poe.txt
you will see that it is the short story “The Cask of Amontillado” by Edgar Allan Poe.
You can press 'q' to exit the more application.
Task: Complete Words.java
• Goal: When completed, Words.java should open the text file poe.txt and count the
number of lines and words in the file. poe.txt contains 2324 words and 330 lines.
• Open the file provided for this lab.:
cd ~/cs170/lab7
gedit Words.java &
• You should see the “skeleton” of a (incomplete) program in your editor. If you see a blank
window, ask your TA for help.
• Begin by compiling this skeleton file. It will tell you that there are 0 words and 0 lines.
• Your task is to complete the main method.
• The comments in the file will guide you in what to do, but here are some steps to take. Be sure
to compile and test frequently.
◦ Hint: Most of the programming needed to complete these instructions can be found in the
lecture note on using a while loop to parse files.
◦ Specify the import statements needed to use Scanner and File objects.
◦ Add the “throws” exception to the main method.
◦ Declare and initialize a File object which is setup to read poe.txt. Since this file is in
your current directory, you can just specify the name of the file (no need to specify an
absolute path).
◦ Declare and initialize a Scanner object to read from your File object.
◦ Remember some helpful Scanner functions:
Method Name
Description
next()
Reads and returns the next token as a String
nextLine()
Reads and returns all the characters up to the next newline ('\n') as a
String
hasNext()
Returns true if there is still a token in the Scanner
hasNextLine() Returns true if there is still at least one line to be read in the
Scanner.
◦ Counting the words in the file:
▪ Write a while loop which is designed to count the words in the poe.txt file. You will use
the next() and hasNext() methods in the Scanner class.
▪ You will also use the predefined wordCount variable to keep track of the count of
words as you iterate over the file using the Scanner methods.
▪ If you have not already, compile and check your work. Make sure you have the wordcount correct before moving on.
◦ Reset your Scanner:
▪ In Java, once you have moved the “pointer” in a Scanner to the end of the file, you
cannot “rewind” the file back to the beginning. You must reset your Scanner by remaking the Scanner object. Add the following line of code to reset your Scanner
(modified for your chosen variable names, obviously).
scannerVar = new Scanner(fileVar);
▪ If you do not do this, your line count will always be 1.
◦ Counting the lines in the file:
▪ Write a while loop which counts the lines in the poe.txt file. You will use the
nextLine() and hasNextLine() methods in the Scanner class.
▪ You will also use the predefined lineCount variable to keep track of the count of
lines as you iterate over the file using the Scanner methods.
▪ Compile and double check your work against the answers given above.
Turning in your work:
• Add your name, userid, and section number as comments to the top of your Words.java file.
• Submit your Words.java file (not the .class file!) to Blackboard for the Lab7 assignment.
If you do not finish during your lab period, the deadline is Saturday at 5pm.
• You can submit as many times as you wish, but we only grade the last submission.
• No late submissions for labs!
Download