0

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!

ordanj
  • 361
  • 1
  • 8
  • 19
  • I just added that. Sorry I didn't copy correctly the first time. – ordanj Apr 18 '14 at 20:16
  • I mean ... you have to import the exception; it's a class just like anything else. – Brian Roach Apr 18 '14 at 20:16
  • Import it. What's the bad practice part? – Dave Newton Apr 18 '14 at 20:16
  • @DaveNewton catching an unchecked exception, probably – fge Apr 18 '14 at 20:17
  • 2
    Have you imported it? (`java.util.NoSuchElementException`) – fge Apr 18 '14 at 20:17
  • 3
    From everything I've read you shouldn't catch a NoSuchElementException because it is a runtime exception and instead you should use hasNext(). – ordanj Apr 18 '14 at 20:17
  • You can lookup in which package a class is in the [Java API documentation](http://docs.oracle.com/javase/8/docs/api/). – Jesper Apr 18 '14 at 20:18
  • I didn't import it. That fixed the problem. Why do you have to import NoSuchElement but not NumberFormatException? NegativeValueException is a custom exception that I wrote so I understand why that doesn't need to be imported. – ordanj Apr 18 '14 at 20:19
  • possible duplicate of [how to catch a NoSuchElementException?](http://stackoverflow.com/questions/5672134/how-to-catch-a-nosuchelementexception) – Shreyos Adikari Apr 18 '14 at 20:20
  • I read that question already and it didn't answer what I was asking. – ordanj Apr 18 '14 at 20:20
  • fge thanks for the answer, that solved the problem. If you want to write an answer I'll select it. – ordanj Apr 18 '14 at 20:22
  • 1
    Catching an unchecked exception isn't intrinsically at bad – Dave Newton Apr 18 '14 at 20:29
  • To answer your follow-up question: the fully qualified name of `NumberFormatException` is `java.lang.NumberFormatException` and to save typing, everything in the `java.lang` package is imported by default. Everything else you need to import unless it's in the same package as the code that uses it (as is the case with your custom `NegativeValueException`). – biziclop Apr 18 '14 at 20:37
  • @Dave You should try to call the method instead of catching the exception when possible. There is a slight performance penalty for catching exceptions because an exception object has to be instantiated. – Michael Apr 18 '14 at 21:39
  • @Michael Exceptions already indicate an exceptional circumstance, so the performance penalty is assumed. My point is that catching an RTE isn't intrinsically bad if that's the API's behavior. If there are other solutions, they should be tested for performance, *if* it's determined it's *actually* an issue in overall application performance. Which I find unlikely in this case. – Dave Newton Apr 18 '14 at 22:08
  • No need for that; and @biziclop explained the problem here: all of `java.lang` is automatically imported and `NumberFormatException` is in `java.lang`. – fge Apr 18 '14 at 22:40
  • @Dave Agreed. I was just thinking of examples like `NoSuchElementException` (you should call `hasNext()` instead) and `IndexOutOfBoundsException` (you should always check an array's bounds yourself). – Michael Apr 19 '14 at 15:08

0 Answers0