Java Reference
In-Depth Information
Display 7.7
Enhanced
StringTokenizer
(part 2 of 2)
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
32
class, and changes the delimiter set in the same way as does the
33
same method in the 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.
37 String token = super.nextToken(delimiters);
38 a[count] = token;
36 {
39 count++;
40
return
token;
super.nextTokens
is the version of
nextToken
defined in the base class
StringTokenizer
.
41 }
/**
42
43
Returns an array of all tokens produced so far. Array returned
44
has length equal to the number of tokens produced so far.
*/
45
46 public String[] tokensSoFar()
48 String[] arrayToReturn = new String[count];
49
47 {
for
(
int
i = 0; i < count; i++)
50 arrayToReturn[i] = a[i];
51
return
arrayToReturn;
tokensSoFar
is a new method.
52 }
53 }
Display 7.8
Use of the
EnhancedStringTokenizer
Class
(part 1 of 2)
1
import java.util.Scanner;
2
public class
EnhancedStringTokenizerDemo
3 {
4
public static void
main(String[] args)
5 {
6 Scanner keyboard =
new
Scanner(System.in);
7 System.out.println("Enter a sentence:");
8 String sentence = keyboard.nextLine();
(continued)