logo CodeStepByStep logo

WordCount

Language/Type: Java collections ArrayList
Related Links:

Write a console program named WordCount that reads a file and reports how many lines, words, and characters appear in it. Suppose, for example, that the file lear.txt contains the following passage from Shakespeare's King Lear:

Poor naked wretches, wheresoe'er you are,
That bide the pelting of this pitiless storm,
How shall your houseless heads and unfed sides,
Your loop'd and window'd raggedness, defend you
From seasons such as these?  O, I have ta'en
Too little care of this!

Given this file, your program should be able to generate the following sample run (with user input shown like this):

File: lear.txt
Lines = 6
Words = 47
Chars = 248

For the purposes of this program, a word consists of a consecutive sequence of letters and/or digits, which you can test using the static method Character.isLetterOrDigit.

Class: Write a complete Java class.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.