Need help with PascalTriangle

• 11-03-2010, 02:49 PM
bap2
Need help with PascalTriangle
Here is my code
Code:

```public class PascalTriangle {     private static int[] ptriangle(StringBuilder outputBuffer, int startRow, int thisRow)     {         if (thisRow == 0)         {           // terminal case           int[] last = new int[1];           last[0] = 1;           return last;         }         int[] nextNum = new int[thisRow + 1];         int[] numAbove = ptriangle(outputBuffer, startRow, thisRow - 1);         // compute padding before the line, (padding width) * (#items this line / 2)         int padding = 4 * (startRow -thisRow) / 2;         padString(outputBuffer, padding, " ");         for (int i = 0; i < thisRow; i++)         {           if (i == 0)           {             nextNum[i] = 1;           }           else if (i == thisRow)           {             nextNum[i] = 0;           }           else           {             nextNum[i] = numAbove[i - 1] + numAbove[i];           }           // right pad the number to 2 decimal places           String number = String.valueOf(nextNum[i]);           padString(outputBuffer, (3 - number.length()), " ");           outputBuffer.append(number).append(" ");         } // for         outputBuffer.append("\n");         return nextNum;     }       /**       * pads a string builder with a specified character a given number of times.       * @param b       * @param padding       * @param character       */       public static void padString(StringBuilder b, int padding, String character)       {         for (int i = 0; i < padding; i++)         {           b.append(character);         }       }       /**       * Convenience wrapper method to initially invoke the recursive method       * @param outputBuffer       * @param startRow       */       public static void ptriangle(StringBuilder outputBuffer, int startRow)       {         ptriangle(outputBuffer, startRow, startRow);       }       private static void usage()       {         System.err.println("Usage: PascalsTriangle <depth>");         System.exit(1);       }       /**       * Generates a pascal's triangle to the specified depth.       * @param args       */       public static void main(String[] args)       {         if (args.length != 1)         {           usage();         }         String strLine = args[0];         int lines = 0;         try         {           lines = Integer.parseInt(strLine);         }         catch (NumberFormatException ex)         {           System.err.println("invalid number: " + strLine);           usage();         }         if (lines < 1)         {           System.err.println("lines must be a number greater than 1.");           usage();         }         StringBuilder outputBuffer = new StringBuilder();         ptriangle(outputBuffer, lines);         System.out.println(outputBuffer.toString());       } }```
And here is what I am getting when I try to run it.
run:
Usage: PascalsTriangle <depth>
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)

Can anyone tell me what I did wrong.
• 11-03-2010, 02:53 PM
KevinWorkman
That looks like you're compiling it but not running it. Post what you're actually doing. How are you trying to run it?
• 11-03-2010, 02:55 PM
JosAH
Quote:

Originally Posted by bap2
Here is my code
[ snip ]
And here is what I am getting when I try to run it.
run:
Usage: PascalsTriangle <depth>
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)

Can anyone tell me what I did wrong.

Is that really your own code? Because what you saw was the output of your usage() method. That method is called when there is not exactly one argument present on the command line.

kind regards,

Jos