public class DoubleScanner
extends java.lang.Object
The ability to return delimiters as tokens is accomplished by providing two classes of delimiters:
Example: Suppose that the Whitespace delimiters are standard
whites space characters and the token delimiters are
+, -, *. /. Then the string:
5 * 10 + 4 / 10 - 1+2-3*4/5
will result in the tokens:
"5", "*", "10", "+",
"4", "/", "10", "-",
"1", "+", "2", "-", "3",
"*", "4", "/", and "5"
being returned by the DoubleScanner.
The DobuleScanner can return boolean, char, double, int, and Strings. In addition, if given a sorted list of "reserved" words, it can "return" reserved words by specifying their subscript in the array. It can easily be extended to handle additional types.
The DoubleScanner operates in the following manner: The Scanner class returns tokens after removing whitespace. The Scanner tokens may include token delimiters. The Scanner's tokens are given to the StringTokenizer along with the string containing token delimiters and told to return the delimiters as well as tokens. It breaks up the scanner tokens into the tokens returned by DoubleScanner.
The default whitespace delimiter used by the Scanner is as recognized by Character.isWhitespace method. Default white space characters include line feed, return, tab and some other control characters in addition to Unicode space, line separator and the paragraph separator.
It is inefficient and normally incorrect to treat the same character as whitespace and as a tokenDelimiter. If a token delimiter character is also treated as a white space, the Scanner never will return the character so DoubleScanner can never return the character as a token.
A scanning operation may block waiting for input.
A tokenDelimiter String is required by every constructor because this class would inefficiently duplicate the action of the Scanner if tokenDelimiters are not needed.
DoubleScanner can easily be extended to handle special types of tokens. For example, the following subclass translates the Spanish words "uno", "dos", and "tres" into "one", "two", and "three" by adding two new methods: hasNextUnoDosTresWords( ) and nextUnoDosTresWords( ).
// **** Imports ****
import java.io.*;
import java.util.*;
public class SubDoubleScanner extends DoubleScanner {
public SubDoubleScanner(String source, String tokenDelimiters) {
super(source, tokenDelimiters);
} // constructor SubDoubleScanner for strings
public boolean hasNextUnoDosTresWords() {
internalNext(); // make sure next token exists and has been found
if (! foundNextToken)
return false;
// is the token one of the special tokens?
if (theNextToken.equals("uno"))
return true;
else if (theNextToken.equals("dos"))
return true;
else if (theNextToken.equals("tres"))
return true;
return false;
} // method hasNextUnoDosTresWords
public String nextUnoDosTresWords() {
String result;
internalNext(); // make sure theNextToken has been found
if (theNextToken == null)
throw new NoSuchElementException(
"DoubleScanner.nextUnoDosTres() cannot find a next token");
// check out theNextToken
if (theNextToken.equals("uno"))
result = "one";
else if (theNextToken.equals("dos"))
result = "two";
else if (theNextToken.equals("tres"))
result = "three";
else
throw new InputMismatchException(
"DoubleScanner.nextUnoDosTres(): "
+ "The next token is not an uno, dos, tres");
foundNextToken = false; // the current token is being used.
return result;
} // method nextUnoDosTresWords
} // class SubDoubleScanner
It is important that each call to a hasNextXxx or nextXXX method begins
with a call to internalNext. That method makes sure that the next token
is stored in theNextToken and foundNextToken is true. If there is no next
token these values are set to null and false. When the token is returned
by a nextXXX method, foundNextToken is set to false to signal that
theNextToken has already been used and the internalNext method will have
to search for the next token the next time it is called.
Revision date: 3/24/2006, correct comment 3/3/2008
| Modifier and Type | Field and Description |
|---|---|
static java.lang.String |
PUNCTUATION
A string containing all the punctuation characters on a standard
English keyboard.
|
| Constructor and Description |
|---|
DoubleScanner(java.io.File source,
java.lang.String tokenDelimiters)
Constructs a new DoubleScanner that produces tokens scanned from
the specified file.
|
DoubleScanner(java.io.InputStream source,
java.lang.String tokenDelimiters)
Constructs a new Scanner that produces tokens scanned from the specified
input stream.
|
DoubleScanner(java.lang.String source,
java.lang.String tokenDelimiters)
Constructs a new Scanner that produces tokens scanned from the
specified string.
|
| Modifier and Type | Method and Description |
|---|---|
java.lang.String[] |
getReservedWords()
Returns the array of reserved words.
|
java.lang.String |
getTokenDelimiters()
Returns the tokenDelimiters String.
|
java.lang.String |
getWhiteSpace()
Returns the whiteSpace regular expression as a String.
|
boolean |
hasNext()
Returns true if this scanner has another token in its input.
|
boolean |
hasNextBoolean()
Returns true if the next token in this scanner's input can be interpreted
as a boolean value using a case insensitive pattern created from the
string "true|false".
|
boolean |
hasNextChar()
Returns true if this scanner's has another token and it is a char.
|
boolean |
hasNextDouble()
Returns true if this scanner's has another token and it is a double.
|
boolean |
hasNextInt()
Returns true if this scanner's has another token and it is an integer.
|
boolean |
hasNextReserved()
Returns true if this scanner's has another token and it is a reserved word
in the array of reserved words.
|
java.lang.String |
next()
Returns the next complete token from this scanner.
|
boolean |
nextBoolean()
Scans the next token of the input into a boolean value
("true" or "false") and returns that
value.
|
char |
nextChar()
Scans the next token of the input as a char.
|
double |
nextDouble()
Scans the next token of the input as an double.
|
int |
nextInt()
Scans the next token of the input as an int.
|
int |
nextReserved()
Scans the next token of the input as a reserved word as specified by
a sorted array supplied by the setReservedWords method.
|
void |
setReservedWords(java.lang.String[] reservedWords)
Set the reserved list for hasNextReservedWord and nextReservedWord.
|
void |
setTokenDelimiters(java.lang.String tokenDelimiters)
Sets a new string that contains the characters that are
treated as both tokens and delimiters.
|
void |
setWhiteSpace(java.lang.String whiteSpace)
Sets string that acts as a regular expression for a white space .
|
public static final java.lang.String PUNCTUATION
public DoubleScanner(java.lang.String source,
java.lang.String tokenDelimiters)
source - - The String to be scanned.tokenDelimiters - - The string used for tokenDelimiters.java.lang.NullPointerException - if tokenDelimiters is null.public DoubleScanner(java.io.InputStream source,
java.lang.String tokenDelimiters)
source - - The InputStream to be scanned.tokenDelimiters - - The string used for tokenDelimiters.java.lang.NullPointerException - if tokenDelimiters is null.public DoubleScanner(java.io.File source,
java.lang.String tokenDelimiters)
throws java.io.FileNotFoundException
source - - A file to be scanned.tokenDelimiters - - The string used for tokenDelimiters.java.io.FileNotFoundException - - if source file is not foundjava.lang.NullPointerException - if tokenDelimiters is null.public boolean hasNext()
hasNext() uses the following algorithm:
I. Has another token already been found? If so, return true.
II. Otherwise: Use the internalNext method to find the next
token and return foundNextToken as specified by that
method.
public java.lang.String next()
java.util.NoSuchElementException - if there are no more tokenspublic boolean hasNextChar()
public char nextChar()
Note: hasNextChar( ) and nextChar( ) do not have corresponding methods in the Scanner class.
java.util.InputMismatchException - if the next token is not a charjava.util.NoSuchElementException - if input to the DoubleScanner is exhaustedpublic boolean hasNextBoolean()
public boolean nextBoolean()
java.util.InputMismatchException - if the next token is not a charjava.util.NoSuchElementException - if input to the DoubleScanner is exhaustedpublic boolean hasNextInt()
public int nextInt()
java.util.InputMismatchException - if the next token does not match the
Integer regular expression, or is out of rangejava.util.NoSuchElementException - if input to the DoubleScanner is
exhaustedpublic boolean hasNextDouble()
public double nextDouble()
java.util.InputMismatchException - if the next token does not match the
double regular expression, or is out of rangejava.util.NoSuchElementException - if input to the DoubleScanner is exhaustedpublic boolean hasNextReserved()
The Scanner class does not have a corresponing method.
java.lang.ClassCastException - if the reserved word array is not an array of
Strings.java.lang.NullPointerException - - if the reserved word array has not been set or
is null.public int nextReserved()
java.util.InputMismatchException - if the next token is not in the reserved
word array.java.util.NoSuchElementException - if input to the DoubleScanner is
exhausted.java.lang.ClassCastException - if the reserved word array is not an array of
Strings.public void setWhiteSpace(java.lang.String whiteSpace)
whiteSpace - - The String containing the regular expression
that determines white space.public java.lang.String getWhiteSpace()
public void setTokenDelimiters(java.lang.String tokenDelimiters)
tokenDelimiters - - a string containing the characters
that acts as both tokens and delimiters.public java.lang.String getTokenDelimiters()
public java.lang.String[] getReservedWords()
public void setReservedWords(java.lang.String[] reservedWords)
reservedWords - a sorted list of reserved words;