Java Reference
In-Depth Information
and ordinary characters. You can control the character classes to some
extent, but you don't have as much flexibility as with regular expres-
sions. So some things easily expressed with one class are difficult, or at
best awkward, to express with the other. For example, the built-in abil-
ity to handle comment lines is a boon for StringTokenizer , while using a
scanner on commented text requires explicit, unobvious handling. For
example:
Scanner in = new Scanner(source);
Pattern COMMENT = Pattern.compile("#.*");
String comment;
// ...
while (in.hasNext()) {
if (in.hasNext(COMMENT)) {
comment = in.nextLine();
}
else {
// process other tokens
}
}
This mostly works. The intent is that if we find that the next token
matches a comment, then we skip the rest of the line by using nextLine .
Note that you can't, for example, use a pattern of "#.*$" to try to match
from the comment character to the end of the line, unless you are guar-
anteed there are no delimiters in the comment itself.
The above fails to work because it can act like there were two comments
when there was only one. Consider a non-comment line followed by a
comment line. The input stream might look something like this:
token\n# This is a comment line\ntoken2
After the last token on the non-comment line is processed, the current
input position is just before the line separator that delimited the end of
 
Search WWH ::




Custom Search