The Complete Reference
ThiS chapter continues our discussion of java.util by examining those classes and interfaces that are not part of the collections framework. These include classes that tokenize strings, work with dates, compute random numbers, and observe . events. Also, the java.util.ztp and java.util.jar packages are briefly mentioned at the end of this chapter. Stringtokenizer
The processing of text often consists of parsing a formatted input string. Pnrs Illg is the division of text into a set of discrete parts, or token», which in n certain sequence can a semantic meaning. The String Tokenizer class provides the first step in this parsing process, often called the lex (lexical analyzer). or scanner. Stringtokenizer implements the Enumeration interface. Therefore, given ;11 input string, you can enumerate the individual tokens contained in it using StringTokenizer. To USe StringTokenizer, you specify an input string and a string that contains delimiters. Delimiter» are characters that separate tokens. Each character in the delimiters string is considered a valid delimiter-Cor example, “,;:” sets the delimiters to a comma, semicolon, and colon. TIle default set of delimiters consists of the whitespace characters: space, tab, newline, and carriage return. ‘ The String Tenderizer constructors are shown here:
String Tenderizer (Stinging) String Token (String sir, String delimiters) . StringTokenizer String sir, String delimiters, boolean dc/imAsToken) In all versions, str is the string that will be tokenized. In the first version, the default delimiters are used. In the second and third versions, delimiters is a string that specifics the delimiters. In the third version, if delil/lAsTokelt is true, then the deli miters are also
returned as tokens when the string is parsed. Otherwise, the deli miters arc not returned. Dellmiters a.re not returned as tokens by the first two forms. Once you have created a StringTokenizcr object, thenexfTokenf ) method is used to extract consecutive tokens. The has More Tokens( ) method returns true while there arc more tokens to-be extracted. Since String Tokenizer implements Enumeration; the has More Elements( ) and next Element( ) methods are also implemented, and they act the same as has More Tokens and next Token( ), respectively.
Here is an example that creates a String Tokenizer to parse “key-value pairs. Consecutive set~ of “key=value” pairs are separated by a semicolon. BitSet A BitSet class creates a special type of array that holds bit values. This army can increase in size as needed. This makes it similar to a vector of bits. The BitSet constructors are shown here: · BitSet() £it Set(int size) The first version creates a default object. The second version allows you to specify its initial size (that is, the number of bits that it can hold). All bits are initialized to zero. BitSet implements the Cloneable interface and defines the methods listed in . The output from this program is shown here. When toString( )’converts a BitSet object to its string equivalent, each set bit is represented by its bit position. Cleared bits are not shown