CS170 Spring 2015 Lab 6 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 Lab Preparation: • • Review parsing files using a while loop and a Scanner object: Review the vocabulary of Scanners and tokens. (You do not need to review the character by character parsing algorithm.) Exercise Preparation: • Start a terminal application and prepare your lab6 directory: • • • • ◦ mkdir ~/cs170/lab6 ◦ cp ~cs170001/share/lab6/* ~/cs170/lab6 ◦ cd ~/cs170/lab6 ◦ ls You should see 2 files: Words.java, and poe.txt If you do not see these files, ask for help. 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/lab6 ◦ 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 Declare and initialize a Scanner object to read from your File object. Remember some helpful Scanner functions: Method Name next() nextLine() hasNext() hasNextLine() ◦ ◦ ◦ Description Reads and returns the next token as a String Reads and returns all the characters up to the next newline ('\n') as a String Returns true if there is still a token in the Scanner 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 word-count 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 re-making the Scanner object. Add the following line of code to reset your Scanner (modified for your variable name,s 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: • • Save all your work and turn in Words.java on BlackBoard Remember that it is your responsibility to make sure your work is submitted correctly. We DO NOT accept work submitted via email.