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
31:       following token.
32:       @return the next token or null if there are no more tokens
33:    */
34:    public String nextToken()
35:    {
36:       String r = peekToken();
37:       start = end;
38:       if (start >= input.length()) return r;
39:       if (Character.isDigit(input.charAt(start)))
40:       {
41:          end = start + 1;
42:          while (end < input.length() && 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: }