public class TokenScanner
extends java.lang.Object
implements java.util.Iterator<java.lang.String>
Hint: See the code for WordScanner from lecture for inspiration/help. We very strongly discourage copying any of that code for use here. The job of TokenScanner is not the same as that of WordScanner; any attempts to force WordScanner code to directly work for this class will likely cause more headaches than help.
Constructor and Description |
---|
TokenScanner(java.io.Reader in)
Creates a TokenScanner for the argued 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 the argued String is a valid word
|
static boolean |
isWordCharacter(int c)
Determines whether the argued 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 when
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 hasNext() 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
- If the argued Reader is nullpublic static boolean isWordCharacter(int c)
Valid word characters are letters (according to Character.isLetter) and apostrophes (which are single quotes '\'').
c
- The character to checkpublic static boolean isWord(java.lang.String s)
A valid word is any sequence of only letter (see Character.isLetter) and/or apostrophe characters. Strings that are null or empty are not valid words.
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