Java Reference
In-Depth Information
FIGURE 9-49
J ava 2
v 5 . 0
3 Using String Methods to Count Words
J ava 2
v 5 . 0
Word processing programs also can count and then display the number of words
in a document, in addition to the number of characters. Using methods from the
String class, write a program that will count the number of words which are sepa-
rated by blanks in a String. For simplicity, use strings without punctuation or
other white space characters (tabs, newlines, etc.). Use a JTextArea to allow a user
to enter the text and allow the text area to scroll, if necessary. When the user clicks
a button to count the words, the total number of words counted is displayed in a
text box that cannot be modified by the user. For Java 5.0, use static imports where
appropriate. For an additional challenge, allow words to be separated by multiple
blanks and by one or more newline characters. Figure 9-50 displays sample output.
FIGURE 9-50
4 Using StringBuffers to Modify Strings
Data communication protocols often must wrap data with other characters, sur-
rounding the data with an envelope of characters, much as a letter is within an
envelope when mailed. As the data passes through different layers of the commu-
nication system, additional layers are wrapped and later unwrapped to guide the
data to its intended destination.
Search WWH ::




Custom Search