3 Replies - 1493 Views - Last Post: 12 April 2013 - 10:31 AM Rate Topic: -----

#1 xSouthpaw   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 06-December 12

Identifier Expected Error in Java

Posted 11 April 2013 - 09:39 PM

Hello everyone,
I have a hard time figuring out little errors like this so if you guys can help me I would really appreciate it.

/**
 * The LLObjectNode declaration may (and should) be contained within the same
 * file as the RefList declaration:
 */

class LLObjectNode<T>
{

    private LLObjectNode<T> link;
    private T info;


    public LLObjectNode ( T theInfo )
    {
        info = theInfo;
        link = null;
    }


    public void setInfo ( T theInfo )
    {
        info = theInfo;
    }


    public T getInfo ()
    {
        return info;
    }


    public void setLink ( LLObjectNode<T> theLink )
    {
        link = theLink;
    }


    public LLObjectNode<T> getLink ()
    {
        return link;
    }

}


/**
 * Stores and manipulates a collection of objects in accordance with the
 * ListInterface functionality:
 */
public class RefList<T> implements ListInterface<T>
{

    /**
     * Stores the number of elements contained within this list:
     */
    protected int numElements;


    /**
     * Points to the first node on this list:
     *
     * This variable will be set to null, when the list is empty.
     */
    protected LLObjectNode<T> first;


    /**
     * Used for iteration through this list:
     *
     * This variable will be set to null until a call is made to the reset()
     * method, at which point this variable will point to the first element on
     * this list:
     */
    protected LLObjectNode<T> currentPos;


    /**
     * Used for locating elements on this list:
     */
    //Set to true if find(...) is successful:
    protected boolean found;

    //Points to the found element:
    protected LLObjectNode<T> location;

    //Points to the element preceding the found element:
    protected LLObjectNode<T> previous;


    /**
     * Attempts to find an element contained within this list that exists as a
     * copy of the given object:
     *
     * If the find is successful, the found variable will be set to true, the
     * location variable will point to the found element, and the previous
     * variable will point to the element preceding the found element, unless
     * the found element is the first element on this list.
     */
    protected void find ( T theTarget )
    {
        found = false;
        location = first;
        previous = null;

        while ( location != null )
        {
            if ( location.getInfo().equals( theTarget ) )
            {
                found = true;

                break;
            }
            else
            {
                previous = location;
                location = location.getLink();
            }
        }
    }


    /**
     * Creates an empty list:
     */
    public RefList<T>()
    {
        currentPos = null;
        first = null;
        numElements = 0;
    }


    /**
     * Returns the number of elements contained within this list:
     */
    public int size()
    {
        return numElements;
    }


    /**
     * Returns true if this list contains a copy of the given object:
     */
    public boolean contains ( T theTarget )
    {
        find( theTarget );

        return found;
    }


    /**
     * Removes the first element found within this list that exists as a copy
     * of the given object and returns true of such an element was found:
     */
    public boolean remove ( T theTarget )
    {
        find( theTarget );

        if ( found )
        {
            if ( location == first )
                first = first.getLink();
            else
                previous.setLink( location.getLink() );

            --numElements;
        }

        return found;
    }


    /**
     * Returns a reference to the first element found within this list that
     * exists as a copy of the given object or null if no such element was
     * found:
     */
    public T get ( T theTarget )
    {
        find( theTarget );

        if ( found )
            return location.getInfo();
        else
            return null;
    }


    /**
     * Returns a nicely formatted string representation of this list:
     */
    public String toString ()
    {
        LLObjectNode node = first;
        StringBuffer buff = new StringBuffer ( "List:" );

        while ( node != null )
        {
            buff.append( " " + node.getInfo() );
            node = node.getLink();
        }

        return buff.toString();
    }


    /**
     * Prints the contents of this list to the screen:
     */
    public void writeLinkedList ()
    {
        System.out.println( toString() );
    }


    /**
     * Initializes this list for iteration:
     */
    public void reset ()
    {
        currentPos = first;
    }

    /**
     * Returns a reference to the element located at currentPos and increments
     * currentPos to point to the next element contained within this list:
     *
     * If currentPos is pointing to the last element in this list, it will be
     * reset to point to the first element in this list.
     *
     * @Preconditions:
     *     This list is not empty.
     *     This list has been reset.
     *     This list has not been modified since the last reset.
     */
    public T getNext ()
    {
        Object next = currentPos.getInfo();

        if ( currentPos.getLink() == null )
            currentPos = first;
        else
            currentPos = currentPos.getLink();

        return next;
    }


    /**
     * Inserts the given object onto the front of this list:
     */
    public void add ( T theObject )
    {
        LLObjectNode<T> node = new LLObjectNode<T>( theObject );

        node.setLink( first );
        first = node;
        ++numElements;
    }
}



Documents\RefList.java:125: error: <identifier> expected
    public RefList<T>();n
                     ^
1 error

Tool completed with exit code 1



I embedded the error above in the code just so everyone knows.I'm sure it's something simple but I have always had a hard time identifying errors like this. On a side note this code wasnt written by me, it was supplied by the instructor to help us with our current assignment which is the list ADT. So I've emailed him about it but it's been a day and I haven't received a response. So i figured I would ask for some help from dream in code. Thanks for any help.

Is This A Good Question/Topic? 0
  • +

Replies To: Identifier Expected Error in Java

#2 natecat   User is offline

  • D.I.C Head

Reputation: 57
  • View blog
  • Posts: 233
  • Joined: 19-December 11

Re: Identifier Expected Error in Java

Posted 11 April 2013 - 11:08 PM

You don't put the type parameters with the constructor when declaring generics.
http://docs.oracle.c...rics/types.html
Was This Post Helpful? 0
  • +
  • -

#3 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Identifier Expected Error in Java

Posted 12 April 2013 - 03:55 AM

Very very very very bad idea tho have a variable name inside a class with the same name as the class
Was This Post Helpful? 0
  • +
  • -

#4 xSouthpaw   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 06-December 12

Re: Identifier Expected Error in Java

Posted 12 April 2013 - 10:31 AM

I cant change much of anything in this code because it's supplied by the teacher to his specification. He finally emailed me last night with the fixed reflist.java code. So thanks for the help.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1