title: Building a string recursively in Java
body: <p>I am trying to build a string recursively but it isn't quite working</p>
  
  <p>my code looks like this</p>
  
  <pre><code>    public void UpdatePrintList(ArrayList&lt;Node&gt; closedList, ArrayList&lt;Node&gt; openList)
      {
          if(count &lt;= iterations)
          {
              String line1 = "";
  
              for(int i = 0; i &lt; closedList.size(); i++)
              {
                  if(i &gt; 0)
                  {
                      line1 = line1 + "-";
                  }
                  line1 = line1 + closedList.get(i).GetMovement();
              }
  
              line1 = line1 + " " + closedList.get(closedList.size()-1).GetG() + " " + closedList.get(closedList.size()-1).GetHeuristic() + " " + closedList.get(closedList.size()-1).GetF();
              printList.add(line1);            
              //*****************************************************************                
  String line2 = "OPEN ";
  
          for(int i = 0; i &lt; openList.size(); i++)
          {
              line2 = FindEarlierNode(openList.get(i), line2);
          }
          System.out.println(line2);
      }
      count++;
  }
  
  private String FindEarlierNode(Node varNode, String varString)
  {
      if(varNode.OpenedBy() == null)
      {
          varString += varNode.GetMovement() + "-";
      }
      else
      {
          FindEarlierNode(varNode.OpenedBy(), varString);
      }
      varString = varString + varNode.GetMovement() + "-";
      return varString;
  }
  </code></pre>
  
  <p>The strange thing is that I know that this if statement </p>
  
  <pre><code>        if(varNode.OpenedBy() == null)
      {
          varString += varNode.GetMovement() + "-";
      }
  </code></pre>
  
  <p>runs correctly, so the function does reach the earliest node. But it doesnt add to the string. The code runs but returns nothing. GetMovement just returns a one or two character string. The output should look like this:</p>
  
  <p>OPEN S-R S-RD S-D</p>
  
  <p>But instead it looks like this:</p>
  
  <p>OPEN D-DL-L-</p>
  
  <p>Can anyone help?</p>