Lab Exercise #4
for lab meeting Wednesday, Sept. 24, 2003
(This repeats last week's lab; try to complete it this week!)
Objectives: The purpose of this lab is to experiment with the
Java
StringTokenizer class. We will use this later in a project.
Copy and examine programs53/CountWords.java. If you
have your
Lewis/Loftus book with you, this is explained on p. 169ff. Compile and
run this program, entering various sentences.
Using your web browser, visit the Sun API site (which is linked
from
the
course web page) and look up the methods in java.util.StringTokenizer.
Note that these methods provide a set of active-iteration controls for
reading tokens from a string one at a time.
Now modify CountWords so that NextToken
treats the
set
of English punctuation characters as tokens and counts the punctuation
characters (but not bkanks) separately from the words. Recompile and
rerun
the program and test this out.
If you have time, investigate Integer.parseInt() and Double.parseDouble().
See the Sun Integer and Double API's for details.
Try modifying CountWords so it counts not only the words but
also the numeric tokens (integer and float). Note that parseInt
and parseDouble throw an exception if the string input isn't
the right kind of token; you can try catching exceptions in order to
develop a parsing strategy that checks whether a token is a double; if
it's not, check whether it's an integer. If the token is neither one of
these, it's a word.