package histogram;
import java.util.*;
import java.io.*;
/**
* WordScanner
*
* Provides a String iterator for a Reader. Objects of this class
* scan through the input, returning the sequence of legal words.
*
* A *word* is a string of letters (upper and lower case A-Z) that does not
* include any whitespace, numbers or punctuation characters.
*
* The WordScanner returns the words in the order found, skipping any
* punctuation, numbers or whitespace from the input.
*
*
*/
public class WordScanner implements Iterator{
private Reader r;
private int c;
public WordScanner (Reader initR) throws IOException {
if (initR == null) {
throw new NullPointerException();
}
r = initR;
skipNonLetters();
}
private void skipNonLetters() throws IOException {
c = r.read();
while (!Character.isLetter(c) ) {
if (c == -1) {
return;
}
c = r.read();
}
}
@Override
public boolean hasNext() {
return (Character.isLetter(c));
}
// Return the next word in the input
// the returned string should only contain letters
@Override
public String next() {
// c is a letter
try {
String s = "";
while (Character.isLetter(c)) {
s = s + (char)c;
c = r.read();
}
skipNonLetters();
return s;
} catch (IOException e) {
throw new NoSuchElementException();
}
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
}