title: infinite loop after removing node from user defined linked list (java)
body: <pre><code>public void dropCourse(String c)
  {
      if(isEmpty())
          throw new NoSuchElementException("Empty Schedule");
      Node current = firstNode;
      Node follow = firstNode;
      follow = follow.getNext();
      current = current.getNext();
      while(!current.getClasses().equals(c) &amp;&amp; current != null)
      {
          follow = current.getNext();
          current.setNext(follow.getNext());
          follow = null;
      }
      if(current.getClasses().equals(c))
      {
          follow.setNext(current);
          System.out.println("Class removed");
      }
  }
  </code></pre>
  
  <p>I have this code in a method called <code>dropClass</code> it is supposed to remove the node from the </p>
  
  <p>linked list and it works but when I try to print it in <code>toString</code> I get stuck in an infinite </p>
  
  <p>loop, I found the line of code is <code>follow.setNext(current);</code> that seems to be the cause of the </p>
  
  <p>infinite loop but I dont know what to do to fix, if I take it out it doesn't remove the </p>
  
  <p>node buts prints correctly, here is where the infinite loop is happening</p>
  
  <pre><code>//Reads schedule
  public String toString()
  {
      if(isEmpty())
          return " ";
      else
      {
          String s = " ";
          Node current = firstNode;
          System.out.print("ID:");
          System.out.print(current.getId());
          current = current.getNext();
          while(current != null)
          {
  
              s = s + "Class:"
                    + current.getClasses()
                    + " Section:"
                    + current.getSection()
                    + " Credits:"
                    + current.getCredit()
                    + " ";
              current = current.getNext();
  
          }
          return s;
      }
  }
  </code></pre>
  
  <p>the loop starts after <code>while(current != null)</code>, it works perfectly if I don't remove a node can some one explain what I did wrong when removing the node?</p>
  
  <p>Edit:the setNext methods body</p>
  
  <pre><code>public void setNext(Node next)
  {
      this.next = next;
  }
  </code></pre>
  
  <p>also the loop i'm using to print out the linked lists</p>
  
  <pre><code>            for(int j = 0;j&lt;list.length;j++)
              {
                  one = list[j];
                  System.out.println(one);
              }
  </code></pre>
  
  <p>I should also mention all the linked lists are in an array call list</p>