Space web hosting - 576 Strings and Characters Chapter 10 The condition

576 Strings and Characters Chapter 10 The condition in the if structure on line 23 uses method equals to determine whether the object c1 has the same contents as the object c2 (i.e., the characters inside each object are equal). 10.20 Class StringTokenizer When you read a sentence, your mind breaks the sentence into individual words and punctuation, or tokens, each of which conveys meaning to you. Compilers also perform tokenization. They break up statements into individual pieces like keywords, identifiers, operators and other elements of a programming language. In this section, we study Java s StringTokenizer class (from package java.util), which breaks a string into its component tokens. Tokens are separated from one another by delimiters, typically white- space characters such as blank, tab, newline and carriage return. Other characters can also be used as delimiters to separate tokens. The program in Fig. 10.20 demonstrates class StringTokenizer. The window for class TokenTest displays a JTextField where the user types a sentence to tokenize. Output in this program is displayed in a JTextArea. When the user presses the Enter key in the JTextField, method actionPerformed(lines 37 49) is invoked. Lines 39 40 assign Stringreference stringToTokenize the value in the text in the JTextField returned by calling event.getActionCommand(). Next, lines 41 42 create an instance of class StringTokenizer. This StringTokenizerconstructor takes a Stringargument and creates a StringTokenizer for stringToTokenize that will use the default delimiter string ” ntr” consisting of a space, a newline, a tab and a carriage return for tokenization. There are two other constructors for class StringTokenizer. In the version that takes two String arguments, the second Stringis the delimiter String. In the version that takes three arguments, the second Stringis the delimiter Stringand the third argument (a boolean) determines whether the delimiters are also returned as tokens (only if the argument is true). This is useful if you need to know what the delimiters are. 1 // Fig. 10.20: TokenTest.java 2 // Testing the StringTokenizer class of the java.util package 3 4 // Java core packages 5 import java.util.*; 6 import java.awt.*; 7 import java.awt.event.*; 8 9 // Java extension packages 10 import javax.swing.*; 11 12 public class TokenTest extends JFrame { 13 private JLabel promptLabel; 14 private JTextField inputField; 15 private JTextArea outputArea; 16 Fig. 10.20 Tokenizing strings with a StringTokenizerobject (part 1 of 3). Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

Leave a Reply