makeVectorFromReader() in the ExVectorSample
/**
* Creates a Vector
object from an input stream
* The data of each student is stored in one line in the input stream,
* and each field is separated by a the delimiter specified in the parameter
* DELIM
, for example.
*
* 328_Galileo Galilei_80_
* 123_Albert Einstein_100_
* 926_Isaac Newton_90_
*
* This method uses {@link StringTokenizer} to obtain each field from a line
*
* @param br a {@link Reader} from which Student
* data are read
* @param DELIM a String
with the delimiter used in the stream
* referenced by br
* @return a Vector
with the Student
data stored
* in the stream br
* @throws {@link IOException} if cannot read from the input stream
* @throws {@link StudentFormatException} if receives a badly formed data
*/
public static Vector makeVectorFromReader(Reader r, final String DELIM)
throws IOException, StudentFormatException {
BufferedReader br = new BufferedReader(r);
Vector v = new Vector();
StringTokenizer st;
String line = "";
int number;
String name;
int grade;
try {
line = br.readLine();
while (line != null) {
st = new StringTokenizer(line, DELIM);
number = Integer.parseInt(st.nextToken());
name = st.nextToken();
grade = Integer.parseInt(st.nextToken());
if (st.hasMoreTokens()) {
throw new StudentFormatException(line);
}
v.add(new Student(number, name, grade));
line = br.readLine();
}
} catch (NoSuchElementException nsee) {
throw new StudentFormatException(line);
} catch (NumberFormatException nfe) {
throw new StudentFormatException(line);
}
return v;
}