Lab 16 10 pts Week 7 Due May 13

Lab 16: File Statistics

Reading files, counting patterns, handling exceptions, writing output

file-io scanner-file FileNotFoundException PrintStream
Clone from GitHub
3 checkpoints 5 autograder 2 timeliness
Prerequisites: lesson-3-1

After this lab, you will be able to:

  • Open and read a text file using Scanner and File
  • Handle FileNotFoundException with a try/catch block
  • Count lines and words in a file using line-by-line processing
  • Write results to an output file using PrintStream

What You’re Building

You will write a program that reads a text file and computes statistics: total lines, total words, and the longest line. The results are printed to the console and also written to a separate output file. The program must handle the case where the input file does not exist without crashing.


Concepts and Misconceptions

Concept Common Mistake What the Test Catches
Scanner(new File(...)) Passing the filename string directly to Scanner without wrapping it in a File object Reads the string itself instead of file contents
FileNotFoundException Declaring throws on main instead of catching it; program crashes on missing file Test expects a graceful error message, not a stack trace
Line counting Starting the count at 1 instead of 0, or counting blank lines incorrectly Off-by-one in reported line count
PrintStream Forgetting to close the PrintStream, causing partial or empty output files Output file missing or incomplete

Checkpoints

Checkpoint 1: Open a File and Count Lines

What to do: Prompt the user for a filename. Create a Scanner wrapping a File object. Use a while (scanner.hasNextLine()) loop to count how many lines the file contains. If the file does not exist, catch FileNotFoundException and print an error message — do not let the program crash.

What the test checks: Correct line count for the provided test file. Graceful error message (not a stack trace) when given a nonexistent filename.

Debugging tip: If your count is off by one, check whether you are counting before or after the first nextLine() call. The pattern is: read a line, then increment. If the file-not-found test fails, make sure your catch block prints a message to System.out, not System.err.

Checkpoint 2: Count Words and Find the Longest Line

What to do: Inside the same loop, split each line on whitespace using line.split("\\s+") to count words. Track the longest line by comparing each line’s length to the current maximum. Store the longest line’s text and its length.

What the test checks: Total word count matches expected value. The longest line text and its character count are correct.

Debugging tip: If your word count is too high, check how split("\\s+") handles blank lines — it returns a one-element array containing an empty string. Add a guard: if (!line.isEmpty()) before counting words from the split. If the longest line is wrong, make sure you use line.length() (character count), not the word count.

Checkpoint 3: Write Statistics to an Output File

What to do: After processing, create a PrintStream for an output file (e.g., stats.txt). Write the line count, word count, and longest line information to that file. Close the PrintStream. Also print the same summary to the console.

What the test checks: The output file exists and contains the correct statistics in the expected format.

Debugging tip: If the output file is empty or does not exist, verify that you are constructing new PrintStream(new File("stats.txt")) and calling close() when done. If the file has partial output, make sure you are not accidentally writing to System.out when you intend to write to the PrintStream, or vice versa.


How to Debug

  1. Check the file path. If your program cannot find the input file, print the absolute path with new File(filename).getAbsolutePath() to see where Java is looking. The working directory in Gradle is usually the project root.

  2. Print intermediate values. After reading each line, print the line number and word count for that line. This helps you spot the exact line where your count diverges from expected.

  3. Test with a tiny file. Create a 3-line file where you can verify the statistics by hand. Once the logic is correct, the autograder’s larger files will pass.


Scoring

Component Points Criteria
Checkpoints 3 1 pt each. Binary: the checkpoint test passes or it does not.
Autograder 5 Correctness across all test cases. Partial credit by proportion of tests passed.
Timeliness 2 Full credit if submitted by the due date. 0 if late.
Total 10