title: Trying to add string objects into a linked list in alphabetical order by using compareTo but I am stuck on inserting elements in the middle. Java
body: <pre><code>public void addElement(Object element)
  {
      if(first == null) //empty list 
      {
          addFirst(element);
      }
      else 
      {
          //having these move
          boolean cloud = true;
          LinkedListIterator hamsters = new LinkedListIterator();
          while (hamsters.hasNext() &amp;&amp; cloud) //while there are elements in the list.
          {
              //getting strings to compare
              String str = (String) element; //string entered
              System.out.println(str +" is string I just entered");
              String str2 = (String) hamsters.next();
              System.out.println(str2 +" is string inside the list");
  
              //if string entered is greater than second string, then insert new node. 
              if(str.compareTo(str2) &gt; 0 || str.compareTo(str2) == 0)
              {
                  hamsters.add(element);
                  cloud = false;
              }
          }
      }
  </code></pre>
  
  <p>The problem with this code is that it only compares with the first element (and inserts the element after the first element) in the linked list. So if I enter something like "apple, banana, cat", it will print "apple, cat, banana," when I need it to print, "apple, banana, cat."</p>
  
  <p>Any suggestions? </p>
  
  <p>Edit: Here's the iterator --> <a href="https://gist.github.com/bettyjing/84ee94b73713226ba8ad" rel="nofollow">https://gist.github.com/bettyjing/84ee94b73713226ba8ad</a></p>