Discussion 4 handout
Group members (names & NetIDs)
Objectives
- Open text files given a file path
- Handle I/O exceptions
- Use
Scanner
to read text line by line - Tokenize strings
Preparation: Demo code and word hunt files
Please download dis04-release.zip, extract it to a known location on your computer, and open it as a project in IDEA. This discussion is more lecture-heavy than usual, so slides have been posted in advance to help you follow along. The classes SumTokensDemo
and PhoneDemo
should be similar to the later examples your TA will demonstrate and can be referenced along with the slides during the activity.
Task 1: Word hunt
One group member, the driver, should open “WordHunt.java” in their “dis04” IDEA project. They will be responsible for writing and running code. The other group members will serve as navigators, double-checking what is typed and suggesting solution structures to efficiently achieve the desired goal.
Implement WordHunt.main()
to perform the following task:
- Read 13 lines of text from the file “index.txt” (in the project’s root directory).
- On the 13th line, read 37 space-delimited entries.
- Extract a filename prefix, line number, and word number (dlimited by hyphens) from the 37th entry.
- Append “.txt” to the filename prefix and open the corresponding file. Read the number of lines specified by the index entry, then read the specified number of words within that line.
- Report the last word read.
Run WordHunt
and record both the index entry and the final word on your worksheet.
You may not have time to finish this exercise in class this week. In that case, complete your WordHunt
program for homework, making sure all group members agree on the target word before submiting.