title: Java Recursion printing asterisks from one method call
body: <p>I have an assignment introducing Recursion in Java and I am running into a roadblock.  The assignment requires a recursion method to output a number of lines of a number of asterisks depending on the integer value passed to it.  For example, if 4 is passed in as variable n, the output would have a first line of one asterisk, next line 2 asterisks, next 3 asterisks, next 4, then 4, 3, 2, &amp; 1 going down.</p>
  
  <p>I have been able to complete the first half of the output (not sure if it is optimal though), but have no clue how to get the method to reverse back down.  This is all to be done in one method call with a variable (n) passed to the method.</p>
  
  <p>Here is the method I have so far:</p>
  
  <pre><code>    public static void myMethod(int n)
      {       
      if (n &lt;= 1) {
              System.out.print("*");          
      } else {
          myMethod(n - 1);
          for (int i = 0; i &lt; n; i++) {
              System.out.print("*");
          }
      }
  
      System.out.print("\n"); // new line
      }
  </code></pre>
  
  <p>It is called from main with this:</p>
  
  <pre><code>    myMethod(n);
  </code></pre>
  
  <p>So what I have is a for loop that will print an asterisk on the same line 'n' times.  After the for loop it proceeds to the next line and cycles, changing n.  But I have no idea how to get it to reverse.</p>
  
  <p>My method prints from the method.  My instructor showed me a sample version passing 2 variables (n) and a null string.</p>
  
  <pre><code>    public static String myMethod(int n, String displayStr) {       
          String currentStr = "";
  
      for (int i = 0; i &lt; n; i++)
          currentStr += "*";
          currentStr += "\n";
  
          if (displayStr == null){
              return myMethod((n - 1), currentStr);   
          } // end base case
  
          else if (n &gt; 0){
              return myMethod((n - 1), (currentStr + displayStr + currentStr));
          }
  
          else {
              return displayStr;
          }
      } // end recursion method myMethod
  </code></pre>
  
  <p>His version prints from main using the following code line:</p>
  
  <pre><code>    System.out.println(myMethod(n, null));
  </code></pre>
  
  <p>I have tried his version and it prints the triangle on it's side but the largest line only prints once instead of twice.  I have spent all day trying to alter his to add in a duplicate line in the middle and am starting to think it isn't possible.</p>
  
  <p>Any help would be GREATLY appreciated.  I am at a complete standstill with this.</p>