Java Reference
In-Depth Information
We create a StreamTokenizer object from the reader and then loop, read-
ing tokens from the stream, adding all the numbers found into the bur-
geoning result. When we get to the end of the input, we return the final
sum.
Here is another example that reads an input source, looking for attrib-
utes of the form name = value , and stores them as attributes in Attrib-
utedImpl objects, described in " Implementing Interfaces " on page 127 :
public static Attributed readAttrs(Reader source)
throws IOException
{
StreamTokenizer in = new StreamTokenizer(source);
AttributedImpl attrs = new AttributedImpl();
Attr attr = null;
in.commentChar('#'); // '#' is ignore-to-end comment
in.ordinaryChar('/'); // was original comment char
while (in.nextToken() != StreamTokenizer.TT_EOF) {
if (in.ttype == StreamTokenizer.TT_WORD) {
if (attr != null) {
attr.setValue(in.sval);
attr = null; // used this one up
} else {
attr = new Attr(in.sval);
attrs.add(attr);
}
} else if (in.ttype == '=') {
if (attr == null)
throw new IOException("misplaced '='");
} else {
if (attr == null) // expected a word
throw new IOException("bad Attr name");
attr.setValue(new Double(in.nval));
attr = null;
}
}
return attrs;
}
 
Search WWH ::




Custom Search