public class TokenScanner
extends java.lang.Object
implements java.util.Iterator<java.lang.String>
Hint: See the code for the WordScanner from Lecture.
Constructor and Description |
---|
TokenScanner(java.io.Reader in)
Creates a TokenScanner for a given Reader.
|
Modifier and Type | Method and Description |
---|---|
boolean |
hasNext()
Determines whether there is another token available.
|
static boolean |
isWord(java.lang.String s)
Determines whether a given String is a valid word
|
static boolean |
isWordCharacter(int c)
Determines whether a given character is a valid word character.
|
java.lang.String |
next()
Returns the next token, or throws a NoSuchElementException if none remain.
|
void |
remove()
We don't support this functionality with TokenScanner, but since
the method is required if implementing Iterator, we just
throw new UnsupportedOperationException(); |
public TokenScanner(java.io.Reader in) throws java.io.IOException
As an Iterator, the TokenScanner should only read from the Reader as much as is necessary to determine getNext() and next(). The TokenScanner should NOT read the entire stream and compute all of the tokens in advance.
in
- the source Reader for character datajava.io.IOException
- if there is an error in readingjava.lang.IllegalArgumentException
- when the provided Reader is nullpublic static boolean isWordCharacter(int c)
Valid word characters are letters (according to Character.isLetter) and single quote '\''.
c
- the character to checkpublic static boolean isWord(java.lang.String s)
Valid words are not null or the empty string. They only contain word characters.
s
- the string to checkpublic boolean hasNext()
hasNext
in interface java.util.Iterator<java.lang.String>
public java.lang.String next()
next
in interface java.util.Iterator<java.lang.String>
NoSuchElementException
- when the end of stream is reachedpublic void remove()
throw new UnsupportedOperationException();
remove
in interface java.util.Iterator<java.lang.String>
java.lang.UnsupportedOperationException
- since we do not support
this functionality