Java Reference
In-Depth Information
if (line != null) {
String[] cells = new String[CELLS];
MatchResult match = in.match();
for (int i = 0; i < CELLS; i++)
cells[i] = match.group(i+1);
vals.add(cells);
in.nextLine(); // skip newline
}
else {
throw new IOException("input format error");
}
}
IOException ex = in.ioException();
if (ex!= null)
throw ex;
return vals;
}
Each line of the input is expected to have four values separated by com-
mas (naturally you'd want to adapt the pattern to account for different
numbers of cells in different calls to the method). If the line matches
what is expected then each value is extracted from the match group.
When a match is found, the newline is left in the input so we use
nex-
tLine
to skip over it. As we don't know how many lines there will be, we
use a
List
to accumulate the rows.
The use of a pattern in multiline mode is common when scanning line-
oriented input. The pattern we used has to match from the start of a
line, so we need to include the start-of-line (
^
) boundary marker, and
that only matches actual lines when the pattern is in multiline mode.
The
findWithinHorizon
methods operate similarly to
findInLine
except
that they take an additional
int
parameter that specifies the maximum