Java Reference
In-Depth Information
Display 7.7 EnhancedStringTokenizer (part 2 of 2)
42
/**
43
Returns an array of all tokens produced so far.
44
Array returned has length equal to the number of tokens produced so far.
45
*/
46
public String[] tokensSoFar()
47
{
48
String[] arrayToReturn = new String[count];
49
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();
9
EnhancedStringTokenizer wordFactory =
10
new EnhancedStringTokenizer(sentence);
11
System.out.println("Your sentence with extra blanks deleted:");
12
while (wordFactory.hasMoreTokens())
13
System.out.print(wordFactory.nextToken() + " ");
14
System.out.println();
15
//All tokens have been dispensed.
16
System.out.println("Sentence with each word on a separate line:");
17
String[] token = wordFactory.tokensSoFar();
18
for ( int i = 0; i < token.length; i++)
19
System.out.println(token[i]);
20
}
21
}
(continued)
 
Search WWH ::




Custom Search