next up previous
Next: Linked lists with tail Up: First assignment Previous: First assignment

Word counter

Write a Java program which reads text from a file and prints, to the screen, a table indicating the number of occurrences of words of length 1,2,3, etc. For example
  Whether 'tis nobler in the mind to suffer
should give
    1      0
    2      2
    3      2
    4      1
    5      0
    6      2
    7      1
You will have to decide for yourself what is meant by a word. The simplest approach is to define a word to be any continuous sequence of upper or lower case letters. Thus 'tis is counted as a 3-letter word, with the apostrophe being skipped over along with spaces, punctuation, digits etc. For this purpose you may find it helpful to use the isLetter method from the Character class.

You will need to use an array with elements corresponding to various possible word lengths. When you print out the results, you should only print occurrences up to the maximum length of word that was actually encountered in the file, so the last one printed is always greater than zero. Simple examples of keyboard and file input can be found in the files CharCount.java and EchoLine.java.

Write a program that exercises the method you have written. Apply it to the test file konig.txt.
Show the output as part of your submission.


next up previous
Next: Linked lists with tail Up: First assignment Previous: First assignment
Peter Williams 2005-06-07