Introduction
Command-line tools such as wc and grep read files named as arguments, process them line by line and print a summary. This project builds a richer version in Java, practising java.nio.file, careful exception handling for missing or non-text files, and the Streams API.
Problem statement
--find WORD. For each file report the number of lines (and blank lines), words, unique words, characters and letters; the number of sentences, average words per sentence and reading time at 200 words per minute; the five most frequent non-stop words; the distribution of word lengths; the longest sentence; and, with --find, every line containing the keyword with its line number. Report missing or unreadable files without stopping.Objectives
- Process command-line arguments
- Read files with Files.readAllLines and handle specific IOExceptions
- Tokenise text with regular expressions
- Summarise data with groupingBy and sorted streams
Features
- Several files per run
- Word, sentence and character statistics
- Top words and length histogram
- Keyword search with line numbers
- Graceful handling of missing and non-UTF-8 files
Concepts demonstrated
- Command-line arguments
- Specific exception handling
- Tokenisation
- Frequency analysis
- Text processing
System architecture
main separates file names from the optional --find WORD and calls analyse for each file, catching NoSuchFileException, MalformedInputException and other IOExceptions separately. analyse reads the lines, splits the lower-cased text on non-word characters into words, splits sentences with a look-behind regex on ., ! or ?, and computes statistics with streams: frequency via groupingBy(identity, counting()), lengths via a TreeMap, and the longest sentence via max.
Folder structure
Complete source code
src/Main.java
What each file does
- src/Main.java
- Argument handling, per-file exception handling, and the statistics in
analyse.
How to run it
Compile
Run
Test against the expected output
Expected output
Generated by running the project with tests/sample-input.txt. Your output should match tests/expected-output.txt exactly.
Testing
- The test passes the arguments in
tests/args.txt; compare the output withtests/expected-output.txt. javais the most frequent word (6 times) in java-history.txt; the haiku has no matching lines.- data/missing.txt is reported as 'File not found' and the program continues.
Common errors
MalformedInputException on some files
The file isn't valid UTF-8; catch the exception or read with the file's real charset.
"don't" is split into don and t
Include the apostrophe in the word pattern ([^a-z0-9']+) and trim apostrophes at the ends.
Possible improvements
- Accept a directory and analyse every .txt file in it
- Detect the language from stop-word frequencies
- Output the report as JSON with a --json flag
Additional challenges
- 1Compute the Flesch reading-ease score.
- 2Find the most common two-word phrases.
- 3Highlight the keyword inside matching lines.
Built and tested it yourself? Mark it to update your dashboard.