title: Created Linked List not returning correct index
body: <p>I created a list that goes {50, 10, 60, 30, 40} and want to return the index when I reach 30 in my Linked List, however my program always returns -1 (basically it is unable to increment and return my index). I am not sure about how to go about getting back the index in a different manor unless I am able to cast the anEntry object into an integer and equal it my index.</p>
  
  <pre><code>class MyLinkedList {
      private Node firstNode; // index = 0
      private int length;
      public MyLinkedList() {
          firstNode = null;
          length = 0;
      } // end default constructor
      /** Task: Adds a new entry to the end of the list.
       * @param newEntry the object to be added as a new entry
       * @return true if the addition is successful, or false if not */
      public boolean add(Object newEntry) {
          Node newNode = new Node(newEntry);
          if (isEmpty())
              firstNode = newNode;
          else {
              Node lastNode = getNode(length-1);
              lastNode.next = newNode;
          }
          length++;
          return true;
      } // end add
      /** Task: Adds a new entry at a specified index
       * @param newEntry the object to be added at the specified index
       * @return true if successful, or false if not */
      public boolean add(int index, Object newEntry) {
          boolean isSuccessful = true;
          if ((index &gt;= 0) &amp;&amp; (index &lt;= length)) {
              Node newNode = new Node(newEntry);
              if (isEmpty() || (index == 0)) {
                  newNode.next = firstNode;
                  firstNode = newNode;
              }
              else {
                  Node nodeBefore = getNode(index - 1);
                  Node nodeAfter = nodeBefore.next;
                  newNode.next = nodeAfter;
                  nodeBefore.next = newNode;
              }
              length++;
          }
          else
              isSuccessful = false;
          return isSuccessful;
      } // end add
      /** Task: Determines whether the list contains a given entry.
       * @param anEntry the object that is the desired entry
       * @return true if the list contains anEntry, or false if not */
      public boolean contains(Object anEntry) {
          boolean found = false;
          Node currentNode = firstNode;
          while (!found &amp;&amp; (currentNode != null)) {
              if (anEntry.equals(currentNode.data))
                  found = true;
              else
                  currentNode = currentNode.next;
          } // end while
          return found;
      } // end contains
      /** Task: Gets an entry in the list.
       * @param the desired index
       * @return the desired entry, or
       * null if either the list is empty or index is invalid */
      public Object getEntry(int index) {
          Object result = null; // result to return
          if (!isEmpty() &amp;&amp; (index &gt;= 0) &amp;&amp; (index &lt; length))
              result = getNode(index).data;
          return result;
      } // end getEntry
      /** Task: Gets index of an entry in the list.
       * @param the desired entry
       * @return index of the first occurrence of the specified entry
       * in this list, or -1 if this list does not contain the entry */
      public int getIndex(Object anEntry) {
          Node currentNode = firstNode;
          int index = 0; // result to return
          while (anEntry != currentNode.data) {
             currentNode = currentNode.next;
             index++;
             if (anEntry.equals(currentNode.data)){
                 break;
             }
             else {
                 return -1;
             }
          }
          return index;
      } // end getIndex
      private Node getNode(int index) {
          Node currentNode = firstNode;
          // traverse the list to locate the desired node
          for (int counter = 0; counter &lt; index; counter++)
              currentNode = currentNode.next;
          return currentNode;
      } // end getNode
          private Node getNode(int index) {
          Node currentNode = firstNode;
          // traverse the list to locate the desired node
          for (int counter = 0; counter &lt; index; counter++)
              currentNode = currentNode.next;
          return currentNode;
      } // end getNode
      private class Node {
          private Object data; // data portion
          private Node next; // link to next node
  
          private Node(Object dataPortion) {
              data = dataPortion;
              next = null;
          } // end constructor
          private Node(Object dataPortion, Node nextNode) {
              data = dataPortion;
              next = nextNode;
          } // end constructor
          private void setData(Object dataPortion) {
              data = dataPortion;
          } // end setData
          private Object getData() {
              return data;
          } // end getData
          private void setNextNode(Node nextNode) {
              next = nextNode;
          } // end setNextNode
          private Node getNextNode() {
              return next;
          } // end getNextNode
      } // end Node
  } // end MyLinkedList
  </code></pre>