Sorted Linked List using comparable in Java
<p>I am writing a ordered linked list for an assignment.  We are using comparable, and I am struggling to get boolean add to work properly.  I have labored over this code for two weeks now, and I am going cross-eyed looking at the code.  I could really appreciate a fresh set of eyes on my code.  </p>

<p>The code should work for Comparable data - both ints and String (not mixed though).  I can get close to making each work, but not one code that stands for all.  Please help me fix this, so the code works for either Strings or Ints.  </p>

<p><strong>I am only allowed to alter the add(), remove() and OrderedListNode classes</strong></p>

<p><strong>Update</strong> Thanks to parkydr, I was able to work out some of my issues, however, I am still getting a null point error.  I am testing both int and Strings.  If the String loop has a "&lt;" in the while section then elements come back in reverse order.  I will be an error for ints with that though.  If I have >=, like parkydr said, then I get back the ints in proper order, but Strings get a null pointer error.  How do I get both to work together?</p>

<p><strong>Update2</strong> the ints need to be in order, like in the code from AmitG.  </p>

<p><strong>Edit</strong> Does anyone have any ideas?  </p>

<pre><code>package dataStructures;

/**
*   Class OrderedLinkedList.
*
*   This class functions as a linked list, but ensures items are stored in ascending     
    order.
*
*/
public class OrderedLinkedList
{

/**************************************************************************
 * Constants
 *************************************************************************/

/** return value for unsuccessful searches */
private static final OrderedListNode NOT_FOUND = null;


/**************************************************************************
 * Attributes
 *************************************************************************/

/** current number of items in list */
private int theSize;

/** reference to list header node */
private OrderedListNode head;

/** reference to list tail node */
private OrderedListNode tail;

/** current number of modifications to list */
private int modCount;


/**************************************************************************
 * Constructors
 *************************************************************************/


/**
 *  Create an instance of OrderedLinkedList.
 *
 */
public OrderedLinkedList()
{
    // empty this OrderedLinkedList
    clear();

}


/**************************************************************************
 * Methods
 *************************************************************************/


/*
 *  Add the specified item to this OrderedLinkedList.
 *
 *  @param  obj     the item to be added
 */
public boolean add(Comparable obj){
   OrderedListNode node = new OrderedListNode(obj);
   OrderedListNode head2 = new OrderedListNode(obj);
   OrderedListNode tail2 = new OrderedListNode(obj);
       if (head2 == null)
       {
           head2 = node;
           tail2 = node;
           return true;
       }


       // When the element to be added is less than the first element in the list
       if (obj.compareTo(head2.theItem) &lt; 0)
       {
           node.next = head2;
           head2 = node;
           return true;
       }

       // When the element to be added is greater than every element in in list
       // and has to be added at end of the list
       if (obj.compareTo(tail2.theItem) &gt; 0)
       {
           tail2.next = node;
           tail2 = node;
           return true;
       }

       //When the element to be added lies between other elements in the list
       if (obj.compareTo(head2.theItem) &gt;= 0 &amp;&amp; obj.compareTo(tail2.theItem) &lt;= 0)
       {
          OrderedListNode current = head.next;
          OrderedListNode previous = head;
          while (obj.compareTo(current.theItem) &gt;= 0)
          {
              previous = current;
              current = current.next;
          }
          previous.next = node;
          node.next = current;

       }

       return true;
   }  

/*
 *  Remove the first occurrence of the specified item from this   
            OrderedLinkedList.
 *
 *  @param  obj     the item to be removed
 */
public boolean remove(Comparable obj)
{
   OrderedListNode curr = head;
       OrderedListNode prev = head;

       while(curr != null &amp;&amp; ! (curr.theItem.compareTo(obj) == 0)){
       prev = curr;
       curr = curr.next;
       }
       if(curr == null)
      return false;
       else{
          prev.next = curr.next;
          curr = null;
          return true;
     }  
  }



/**
 *  Empty this OrderedLinkedList.
 */
public void clear()
{
    // reset header node
    head = new OrderedListNode("HEAD", null, null);

        // reset tail node
        tail = new OrderedListNode("TAIL", head, null);

        // header references tail in an empty LinkedList
        head.next = tail;

        // reset size to 0
    theSize = 0;

    // emptying list counts as a modification
    modCount++;
}


/**
 *  Return true if this OrderedLinkedList contains 0 items.
 */
public boolean isEmpty()
{
    return theSize == 0;
}


/**
 *  Return the number of items in this OrderedLinkedList.
 */
public int size()
{
    return theSize;
}


/*  
 *  Return a String representation of this OrderedLinkedList.
 *
 *  (non-Javadoc)
 *  @see java.lang.Object#toString()
 */
@Override
public String toString()
{
    String s = "";

    OrderedListNode currentNode = head.next;

    while (currentNode != tail)
    {
        s += currentNode.theItem.toString();

        if (currentNode.next != tail)
        {
            s += ", ";
        }

        currentNode = currentNode.next;
    }

    return s;
}


/**************************************************************************
 * Inner Classes
 *************************************************************************/


/**
 *  Nested class OrderedListNode.
 *
 *  Encapsulates the fundamental building block of an OrderedLinkedList
 *  contains a data item, and references to both the next and previous nodes
 *  in the list
 */


// TODO: Implement the nested class OrderedListNode (5 points).  This nested class
// should be similar to the nested class ListNode of the class LinkedList, but
// should store a data item of type Comparable rather than Object.
    public static class OrderedListNode {


    Comparable theItem;   
        OrderedListNode next;
        OrderedListNode prev;


        OrderedListNode( Comparable theItem ) { this( theItem, null, null ); }

        OrderedListNode( Comparable theItem, OrderedListNode prev, OrderedListNode next)
        {
           this.theItem = theItem;         
           this.next = next;        
           this.prev = prev;
        }

        Comparable getData() { return theItem; }

        OrderedListNode getNext() { return next; }

        OrderedListNode getPrev() { return prev; }

        }
  // Remove - for testing only
   public static void main (String[] args)
   {
     OrderedLinkedList list = new OrderedLinkedList();
     list.add("1");
     list.add("4");
     list.add("3");
     list.add("33");
     list.add("4");
     System.out.println(list.toString());

   }

 }
</code></pre>

<p>This above code works for ints for the most part except that items are stored as strings lexically.  So I need help fixing that.  I also need to make this code work with Strings as well.  Right now the below code works with String but not ints, it also stores in reverse order since the &lt;= changes in the while statement.  Help!</p>

<p>Notice that the change in sign will make Strings work (albeit in reverse order):</p>

<pre><code>  while (obj.compareTo(current.theItem) &lt;= 0)
</code></pre>
