# [Q] Spacing Help

• 02-08-2011, 01:15 AM
iriscience
[Q] Spacing Help
Hello! I am having serious issues with trying to get the spacing on my homework to look right. I have to print out a triangle that looks like this:
Code:

```                                  1                               1  2  1                           1  2  4  2  1                       1  2  4  8  4  2  1                   1  2  4  8  16  8  4  2  1               1  2  4  8  16  32  16  8  4  2  1           1  2  4  8  16  32  64  32  16  8  4  2  1       1  2  4  8  16  32  64 128  64  32  16  8  4  2  1```
I can get the triangle to print the numbers correctly and form the left side of the triangle. I just cannot get the numbers to line up! Any help is greatly appreciated! Thanks guys!

PS: Here is the code I have so far. The object was to make it using for loops.
Code:

```  public static void main(String[] args)   {       //Declare variables       int lengthOfLine;       //Get input from user       Scanner inputFromUser = new Scanner(System.in);       System.out.print("Enter a positive number: ");       int height = inputFromUser.nextInt();       lengthOfLine = 1;       for (int count = 0; count < height; count++)       {         for (int numberOfSpacesForLine = 1; numberOfSpacesForLine                 < height - count; numberOfSpacesForLine++)         {             if ()             System.out.print(" ");         }         int numberToPrint = 1;         for (int loopCount = 0; loopCount < lengthOfLine; loopCount++)         {             System.out.print(numberToPrint);             if (loopCount < ((lengthOfLine - 1) / 2.0))             {               numberToPrint *= 2;             } else             {               numberToPrint  /= 2;             }         }         lengthOfLine += 2;         System.out.println();       }   } }```
• 02-08-2011, 01:19 AM
Junky
You can use String.format or the printf method.
• 02-08-2011, 01:57 AM
iriscience
It should be printf(%5d , numberToBePrinted); right?
• 02-08-2011, 01:58 AM
Junky
Did you try it?
• 02-08-2011, 02:09 AM
iriscience
No, I haven't gotten a chance. I'm delivering pizzas and I had a chance to post my question real quick at the beginning of my shift. I did ask that badly. I meant *should* that work? If not, I don't expect you to just give me the answer. I was just hoping I'm going the right direction with it. Thanks so much for your help!
• 02-08-2011, 02:19 AM
Junky
Well it looks like it might be correct but the best way to find out is when you get on the computer and test if it does what you want.
• 02-08-2011, 02:24 AM
Fubarable
You shouldn't be asking these questions if you're not able to first test them on your computer. We are no substitute for a Java compiler.
• 02-08-2011, 02:44 AM
iriscience
Junky, thank you so much for all your help and patience. I got some downtime and got it working. Thanks again!