|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Objectdbrown.Tokenizer
public class Tokenizer
Tokenizer takes an equation string as input and tokenizes it, breaking it up
into integers (the operands) and single characters (the operators). Each call
to nextOperand or nextOperator retrieves the next
token as an integer or a string respectively and removes that token from the
remaining string. When all tokens have been removed the string is empty. The
initial String is assigned by passing it to the Tokenizer constructor.
This methods of this class follow the approach of similar methods in the
Scanner class. Thus nextOperand throws the same
exceptions as those thrown by Scanner.nextInt.
The job of determining whether or not a token is an operator, operand, or a variable is left up to the Operator, Operand, and Variable classes respectively. This allows the definition of an operator, operand, or a variable to be changed without the necessity to change the Tokenizer class.
Note that the tokenizer does not distinguish between a minus sign used as an operator and a minus sign at the beginning of an operand value. Thus the string
45 - 67
could be interpreted either as the operand 45 followed by the operator - and the operand 67, or as the operand 45 followed by the operand -67. Similarly the string 32*-14 could be (mis)interpreted in more than one way. It is up to the programmer to keep track of whether an operator or an operand is next expected in the sequence of tokens.
| Field Summary | |
|---|---|
private boolean |
closed
A flag for determining if the tokenizer is open or closed. |
java.lang.String |
string
The string to tokenize. |
| Constructor Summary | |
|---|---|
Tokenizer(java.lang.String string)
Create a Tokenizer object. |
|
| Method Summary | |
|---|---|
void |
close()
Closes this tokenizer. |
boolean |
hasNext()
Returns true if potentially there are tokens remaining in the tokenizer string. |
boolean |
hasNextOperand()
Returns true if the next token in the tokenizer string can be interpreted as an operand. |
boolean |
hasNextOperator()
Returns true if the next token in the tokenizer string can be interpreted as an operator. |
boolean |
hasNextVariable()
Returns true if the next token in the tokenizer string can be interpreted as a variable. |
Operand |
nextOperand()
Attempts to extract an operand from the beginning of the tokenizer string. |
Operator |
nextOperator()
Attempts to extract an operator from the beginning of the tokenizer string. |
Variable |
nextVariable()
Attempts to extract a variable from the beginning of the tokenizer string. |
void |
skip()
Removes the first character from the tokenizer string only if the character is not part of an operand, operator, or variable. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
|---|
private boolean closed
public java.lang.String string
| Constructor Detail |
|---|
public Tokenizer(java.lang.String string)
Tokenizer object.
string - The string to tokenize.| Method Detail |
|---|
public void close()
null.
Tokens cannot be extracted from a closed tokenizer.
public boolean hasNext()
java.lang.IllegalStateException - - if this tokenizer is closedpublic boolean hasNextOperand()
java.lang.IllegalStateException - - if this tokenizer is closedpublic boolean hasNextOperator()
java.lang.IllegalStateException - - if this tokenizer is closedpublic boolean hasNextVariable()
java.lang.IllegalStateException - - if this tokenizer is closedpublic Operand nextOperand()
java.lang.IllegalStateException - - if this tokenizer is closed
java.util.NoSuchElementException - - if the tokenizer string is exhausted
java.util.InputMismatchException - - if the next token is not an operand (thrown from
'matchResult')public Operator nextOperator()
java.lang.IllegalStateException - - if this tokenizer is closed
java.util.NoSuchElementException - - if the tokenizer string is exhausted
java.util.InputMismatchException - - if the next token is not an operator (thrown from
'matchResult')public Variable nextVariable()
java.lang.IllegalStateException - - if this tokenizer is closed
java.util.NoSuchElementException - - if the tokenizer string is exhausted
java.util.InputMismatchException - - if the next token is not a variable (thrown from
'matchResult')public void skip()
java.lang.IllegalStateException - - if this tokenizer is closed
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||