01: /**
02:    This class breaks up a string describing an expression
03:    into tokens: numbers, parentheses, and operators.
04: */
05: public class ExpressionTokenizer
06: {
07:    /**
08:       Constructs a tokenizer.
09:       @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;
38:       if (Character.isDigit(input.charAt(start)))
39:       {
40:          end = start + 1;
41:          while (end < input.length() 
42:                && Character.isDigit(input.charAt(end)))
43:             end++;
44:       }
45:       else
46:          end = start + 1;
47:       return r;      
48:    }
49: 
50:    private String input;
51:    private int start;
52:    private int end;
53: }