Java Reference
In-Depth Information
2
This class breaks up a string describing an expression
3
into tokens: numbers, parentheses, and operators.
4 */
5
public class
ExpressionTokenizer
6 {
7 /**
8
Constructs a tokenizer.
9 @param anInput
the string to tokenize
10 */
11
public
ExpressionTokenizer(String anInput)
12 {
13 input = anInput;
14 start =
0
;
15 end =
0
;
16 nextToken();
17 }
18
19 /**
20
Peeks at the next token without consuming it.
21 @return
the next token or null if there are no more
tokens
22 */
23
public
String peekToken()
24 {
25
if
(start >= input.length())
return
null
;
26 else return input.substring(start,
end);
27 }
28
29 /**
30
Gets the next token and moves the tokenizer to the following token.
31 @return
the next token or null if there are no more
tokens
32 */
33
public
String nextToken()
34 {
35 String r = peekToken();
36 start = end;
37
if
(start >= input.length())
return
r;