

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


Java's Application Programming Interface (API) is a collection of hundreds of classes designed to add power and flexibility to the language. The StringTokenizer class is one of these, providing a number of methods for dealing with strings as a sequence of tokens that are separated by delimiters. The most common delimiter is the blank space. Consider the following string: String quote = "To be or not to be"; If blanks are used as delimiters, then this string contains six tokens: "To", "be", "or", "not", "to", and "be". Consider the following code fragment, which assumes the variable quote has already been declared as shown above: StringTokenizer st = new StringTokenizer(quote); // while (st.hasMoreTokens()) // System.out.println(st.nextToken()); // The output will be as shown to the right. The explanation of each line of code is as follows:
delimiters are specified, the default delimiter of a blank space is used.
processed.
object. StringTokenizer Constructors The class has three overloaded constructors, each of which has a String parameter which is used to create a StringTokenizer object. The differences come in their treatment of delimiters.
Creates a StringTokenizer object for the string s. It uses the default delimiter, which is a blank space. It also uses any other white space (newlines, tabs, carriage returns, form feeds) as delimiters.
Creates a StringTokenizer object for the string s. It uses any of the characters from the string d as delimiters. The characters in d will not be treated as tokens.
Same as above, using the string s to create the object, and d to define the delimiters. If the flag is set to true, the characters in d will also be treated as tokens (each characters will be a string of length one). If the flag is false, the delimiters are not treated as tokens. To illustrate the second and third versions of the constructors, consider the following examples. StringTokenizer st2 = new StringTokenizer("http://www.java.sun.com",":/."); Delimiters are the colon (:), backslash (/) and period (.). Tokens are: "http", "www", "java", "sun", "com". To be or not to be
StringTokenizer st3 = new StringTokenizer("12(345+6789)","/+-()",true); Delimiters are the arithmetic operators (, /, +, - ) and the left/right parentheses (). Tokens will include the delimiters, since the flag is set to true. Tokens: "12" "" "(" "345" "+" "6789" ")" StringTokenizer Methods
Returns the next available token from the implicitly referenced StringTokenizer object. Each call causes a reference to move along the string to the position of the next token. If there are no more tokens, calling this method will cause an exception.
Checks to see if there are any more tokens available. Returns true if and only if there is at least one token in the string after the current position.
Returns the number of tokens remaining in the implicit object. Accessing the StringTokenizer Class Almost all classes that we have discussed are part of the core package called java.lang. The contents of this package are available to our programs by default. The StringTokenizer class is part of the java.util package, and must be specified for use in our programs. One way to accomplish this is through the use of an import statement at the beginning of a program. import java.util.StringTokenizer; We can also write a broader import which allows the use of all classes in the java.util package (which can be more useful if we require multiple packages from java.util). import java.util.*;