Java Reference
In-Depth Information
Notice that the period and comma are not part of the tokens produced, because they
are now token separators. Also note that the string of token separators is the second
argument to the constructor.
Some of the methods for the class
StringTokenizer
are summarized in Display
4.17. A sample use of
StringTokenizer
is given in Display 4.18.
Display 4.18
Some Methods in the Class
StringTokenizer
The class
StringTokenizer
is in the
java.util package
.
public
StringTokenizer(String theString)
Constructor for a tokenizer that will use whitespace characters as separators when finding tokens in
theString
.
public
StringTokenizer(String theString, String delimiters)
Constructor for a tokenizer that will use the characters in the string
delimiters
as separators when
finding tokens in
theString
.
public boolean
hasMoreTokens()
Tests whether there are more tokens available from this tokenizer's string. When used in conjunction
with
nextToken
, it returns
true
as long as
nextToken
has not yet returned all the tokens in the
string; returns
false
otherwise.
public
String nextToken()
Returns the next token from this tokenizer's string. (Throws
NoSuchElementException
if there are
no more tokens to return.)
5
public
String nextToken(String delimiters)
First changes the delimiter characters to those in the string
delimiters
. Then returns the next token
from this tokenizer's string. After the invocation is completed, the delimiter characters are those in
the string
delimiters
. (Throws
NoSuchElementException
if there are no more tokens to return.
Throws
NullPointerException
if
delimiters
is
null
.)
5
public int
countTokens()
Returns the number of tokens remaining to be returned by
nextToken
.
5
Exceptions are covered in Chapter 9. You can ignore any reference to
NoSuchElementException
until you reach Chapter 9. We include it here for reference value only.