title: Inserting Node into middle of Linked List, and accidentally inserting null node as well
body: <p>I'm working on a program that does not use Java's built in Linked List class; I'm building it from scratch.  I've been successful with everything except writing a method that inserts a Node into a particular position of the linked list.</p>
  
  <p>I have a method that sets a particular Node as the "current" Node.  So, for example, I have a linked list that looks like this: <strong>cats</strong> --> <strong>dogs</strong> --> <strong>make</strong> --> <strong>good</strong> --> <strong>pets</strong>, and "current" is equal to 2; that means that the "current" Node is "dogs".</p>
  
  <p>From here, let's say I want to insert a new Node at the position of "current" whose info field reads <strong>and</strong>.  If done correctly, the final linked list will be: <strong>cats</strong> --> <strong>and</strong> --> <strong>dogs</strong> --> <strong>make</strong> --> <strong>good</strong> --> <strong>pets</strong>; "and" will replace "dogs" at position 2.  </p>
  
  <p>So here's my problem: my method works to insert a new Node at position two, but something's going wrong with linking the newly created node to pre-existing nodes.  Not only am I inserting my new node into the list, but I'm also inserting a node with no information before "dogs".  As my code currently runs, the output looks like this: <strong>cats</strong> --> <strong>and</strong> --> (blank) --> <strong>dogs</strong> --> <strong>make</strong> --> <strong>good</strong> --> <strong>pets</strong>.</p>
  
  <p>I'm 99.9% sure the problem lies in the (if current != null) portion of the code, I just can't figure out how to fix it. </p>
  
  <p>Any thoughts on why I'm inserting a blank node in addition to the node I actually want to add?</p>
  
  <pre><code>public void insert () {
  
      System.out.println("Please enter the text you wish to insert, or type \"end\" if you are done inserting.");
      String theString;
      theString = console.nextLine();
  
      while (!theString.equals("end")){
          newNode = new Node ();
          newNode.info = theString;
          newNode.next = null;
  
          if (first == null){
              first = newNode;
              last = newNode;
          } else if (current != null){
              Node p = new Node (current.info, current.next);
              current.info = newNode.info;
              current.next = p;
          }
          else {
              last.next = newNode;
              last = newNode;
          }
  
          System.out.println("Please enter the text you wish to insert, or type \"end\" if you are done inserting.");
          theString = console.nextLine();
      }   
  }
  </code></pre>
  
  <p><strong>EDIT</strong></p>
  
  <p>The entire program is quite long, but here is the "setLine" method which sets current equal to whatever position the user wishes to insert their Node at.  It takes a parameter "int line" which is obtained via a user prompt.</p>
  
  <pre><code>public Node setLine(int line) {
  
      int index = 0;
      current = first;
      while (index &lt; line) {
          previous = current;
          current = current.next;
          index++;
      }
      return current;
  }
  </code></pre>