Alex Rivera | Logout

Difficulty throwing NoSuchElementException in Java

Asked 2011-10-02T23:54:19.820
10

this is my first attempt to a code doubly linked program in java:

This is my implementation of getting an iterator to getall items in the doubly linkedlist

public Object next()  {
    if(list.getSize()==0){
     throw new NoSuchElementException();

    }else{ 
        current=current.getNext();
        return current.getItem();
    } 
}

Please dont laugh at me but whatever I try I am getting

Cannot find symbol : symbol class: NoSuchElementException

I tried creating a class NoSuchElementException.java which extends Exception

Then I get

unreported exception NoSuchElementException; must be caught or declared to be thrown throw new NoSuchElementException();

I tried changing code to :

public Object next() throws NoSuchElementException {

Then I get

next() in ElementsIterator cannot implement next() in java.util.Iterator; overridden method does not throw NoSuchElementException

Can anybody point to where I got wrong. If this is not sufficient info to resolve this issue, please do tell me.

Edit
Report

1 Answer

20

You need to import java.util.NoSuchElementException instead of creating your own.

answered 2011-10-02T23:59:36.940

Your Answer