Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
CS170 Lab 6
Fall 2012 Data Files and Loops
Due: Midnight tonight
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:
http://www.mathcs.emory.edu/~cheung/Courses/170/Syllabus/07/file-proc.html
• Review the vocabulary of Scanners and tokens at the beginning of  this lecture:
http://www.mathcs.emory.edu/~cheung/Courses/170/Syllabus/07/parsing.html
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 ~cs170003/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 the 
TA 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 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 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 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 close gedit.
• Turn in your lab 6 work using these commands: 
cd ~/cs170/lab6
/home/cs170xxx/turnin-lab Words.java lab6
◦ Note: you will need to replace 'xxx' with your section number.
• These commands makes a copy of each of your files in the grader's account. 
• Your turnin is successful when you see a success message for each file like: 
Program `Words.java' has been turned in by YOUR_ID as lab6
• If you do not see the “success” message above, ask your TA for help.