Java Reference
In-Depth Information
Display 7.7
EnhancedStringTokenizer
(part 1 of 2)
1
import java.util.StringTokenizer;
2
3
public class
EnhancedStringTokenizer
extends
StringTokenizer
4{
5
private
String[] a;
6
private int
count;
7
public
EnhancedStringTokenizer(String theString)
8
{
The method
countTokens
is inherited
and is not overridden.
9
super
(theString);
10
a =
new
String[countTokens()];
11
count = 0;
12
}
13
public
EnhancedStringTokenizer(String theString, String delimiters)
14
{
15
super
(theString, delimiters);
16
a =
new
String[countTokens()];
17
count = 0;
18
}
19
/**
20
Returns the same value as the same method in the StringTokenizer class,
21
but it also stores data for the method tokensSoFar to use.
22
*/
This method
nextToken
has its definition
overridden.
23
public
String nextToken()
24
{
25
String token =
super
.nextToken();
26
a[count] = token;
super.nextTokens
is the version of
nextToken
defined in the base class
StringTokenizer
. This is explained
more fully in Section 7.3.
27
count++;
28
return
token;
29
}
30
/**
31
Returns the same value as the same method in the StringTokenizer class,
32
changes the delimiter set in the same way as does the same method in the
33
StringTokenizer class, but it also stores data for the method tokensSoFar to use.
34
*/
35
public
String nextToken(String delimiters)
This method
nextToken
also
has its definition overridden.
36
{
37
String token =
super
.nextToken(delimiters);
38
a[count] = token;
39
count++;
super.nextTokens
is the version of
nextToken
defined in the base class
StringTokenizer
.
40
return
token;
41
}