# Thread: [Q] Spacing Help

1. Member
Join Date
Jan 2011
Location
Ohio
Posts
11
Rep Power
0

## [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:
Java 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.
Java 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();
}
}
}```

2. You can use String.format or the printf method.

3. Member
Join Date
Jan 2011
Location
Ohio
Posts
11
Rep Power
0
It should be printf(%5d , numberToBePrinted); right?

4. Did you try it?

5. Member
Join Date
Jan 2011
Location
Ohio
Posts
11
Rep Power
0
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!

6. 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.

7. 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.

8. Member
Join Date
Jan 2011
Location
Ohio
Posts
11
Rep Power
0
Junky, thank you so much for all your help and patience. I got some downtime and got it working. Thanks again!

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•