      1  /**

      2     This class breaks up a string describing an expression

      3     into tokens: numbers, parentheses, and operators.

      4  */

      5  public class ExpressionTokenizer

      6  {

      7     private String input;

      8     private int start; // The start of the current token

      9     private int end; // The position after the end of the current token

     10

     11     /**

     12        Constructs a tokenizer.

     13        @param anInput the string to tokenize

     14     */

     15     public ExpressionTokenizer(String anInput)

     16     {

     17        input = anInput;

     18        start = 0;

     19        end = 0;

     20        nextToken(); // Find the first token

     21     }

     22

     23     /**

     24        Peeks at the next token without consuming it.

     25        @return the next token or null if there are no more tokens

     26     */

     27     public String peekToken()

     28     {

     29        if (start >= input.length()) { return null; }

     30        else { return input.substring(start, end); }

     31     }

     32

     33     /**

     34        Gets the next token and moves the tokenizer to the following token.

     35        @return the next token or null if there are no more tokens

     36     */

     37     public String nextToken()

     38     {

     39        String r = peekToken();

     40        start = end;

     41        if (start >= input.length()) { return r; }

     42        if (Character.isDigit(input.charAt(start)))

     43        {

     44           end = start + 1;

     45           while (end < input.length()

     46                 && Character.isDigit(input.charAt(end)))

     47           {

     48              end++;

     49           }

     50        }

     51        else

     52        {

     53           end = start + 1;

     54        }

     55        return r;

     56     }

     57  }