For starters, I'm aware that this question involves bad coding practice, but my teacher has required it.
Okay, so I'm using .next() of the scanner class and my teacher has specified that I must catch and handle the NoSuchElementException. My code will not compile and I am receiving this error
School.java:98: error: cannot find symbol catch (NoSuchElementException e) { ^ symbol: class NoSuchElementException location: class School
Do I have to import something to catch a NoSuchElementException?
Here's the code in question.
while (file.hasNext()) {
record = file.nextLine();
Scanner info = new Scanner(record).useDelimiter(";");
type = Character.toUpperCase(info.next().trim().charAt(0));
switch(type) {
case 'U':
try {
idNumber = info.next().trim();
name = info.next().trim();
credits = Integer.parseInt(info.next().trim());
residency = Integer.parseInt(info.next().trim());
Undergraduate u = new Undergraduate(idNumber, name, credits,
residency);
addStudent(u);
}
catch (NegativeValueException e) {
addExcludedRecord(e + " occurred while processing:\n" + record);
}
catch (NumberFormatException e) {
addExcludedRecord(e + " occurred while processing:\n" + record);
}
catch (NoSuchElementException e) {
addExcludedRecord(e + " occurred while processing:\n" + record);
}
break;
This is just the first case of the switch because I didn't want to include everything. Here is a sample of a line of the file that is being read by the Scanner.
Ugrad; 1234567890; Jones, Sam; 16; 1
I really appreciate any help!