alexthefourth
alexthefourth

Reputation: 137

Queue linked list front method

Can the return statement not be inside the if statement? When I compile, I get this error:

QueueTestList.java:180: error: missing return statement.

My code:

public coordinate front() 
{
    if(!empty())
    {
    queueNode firstNode = last.getNext();
        return firstNode.getCoord();
    }


}

Upvotes: 0

Views: 969

Answers (1)

Beau Grantham
Beau Grantham

Reputation: 3456

The problem is that if empty() returns true, the method does not have a value to return. The method needs to return a value (or throw an exception) in all cases.

public coordinate front() 
{
    if (empty())
        return null;

    queueNode firstNode = last.getNext();
    return firstNode.getCoord();
}

On a side note, classes should start with a capital letter (Coordinate).

Upvotes: 4

Related Questions