public java.util.Iterator | iterator()This iterator() method simply defines an inner class that
each time hasNext() succeeds, calling next() will
a Term object, containing the term and definition.
return new Iterator() {
String line, term, defn;
/** The hasNext() method returns true up until end of file. */
public boolean hasNext() {
try {
return ((line = is.readLine()) != null);
} catch (IOException e) {
System.err.println("IO Error: " + e);
return false;
}
}
/** The next() method returns the next available Term object */
public Object next() {
int i;
// loop, ignoring invalid lines.
while ((i = line.indexOf("\t"))<0 && hasNext());
;
if (line == null)
throw new IllegalStateException("Invalid EOF state");
term = line.substring(0, i);
defn = line.substring(i+1);
return new Term(term, defn);
}
/** The remove method is not implemented: just throws. */
public void remove() {
throw new UnsupportedOperationException();
}
};
|