# Thread: Why is the spacing so odd when I print this code?

1. Member
Join Date
Apr 2012
Location
St. Louis, MO
Posts
32
Rep Power
0

## Why is the spacing so odd when I print this code?

Java Code:
```import java.text.DecimalFormat;
import java.lang.Math;
public class Table
{
public static void main(String[]args)
{

System.out.println(" #       sqare      square root     cube           cube root");
System.out.println();
DecimalFormat format1 = new DecimalFormat("0.##");
for(double i=0;i<=20;i++)
{

if(i<10)
{
double num1=(Math.pow(i,2));
double num2=(Math.sqrt(i));
double num3=(Math.pow(i,3));
double num4=(Math.cbrt(i));

System.out.println(" "+i+"      "+num1+"             "+format1.format(num2)+"            "+num3+"           "+format1.format(num4));
}
}

}

}```
It prints like the attatched picture, as opposed to in a straight column.

2. ## Re: Why is the spacing so odd when I print this code?

What would you expect to happen? You are printing out different numbers of digits, but then the same amount of spaces. That's going to lead to misaligned columns.

3. Member
Join Date
Apr 2012
Location
St. Louis, MO
Posts
32
Rep Power
0

## Re: Why is the spacing so odd when I print this code?

How would I counteract this?

4. ## Re: Why is the spacing so odd when I print this code?

You can either ensure that you always print out the same number of digits, or you can vary the number of spaces depending on how many digits you print out. Which method you choose really depends on what you're trying to achieve.

5. Member
Join Date
Oct 2012
Location
Tempe, Arizona
Posts
77
Blog Entries
12
Rep Power
0

## Re: Why is the spacing so odd when I print this code?

When making a table, you should use printf instead of println.

This is a snippet of a program that I created a while back:
Java Code:
```System.out.printf("%#17s \t \t %#13s \t %#9.1f \t %#12.1f \t %#12.1f \n",
distro[ix] ._linuxName, distro[ix] ._userFriendly,
distro[ix] ._sizeMB, distro[ix] ._customization,
distro[ix] ._cuttingEdge);```
% is used to identify a variable being placed
# is used to decide the character width of that placement
-#17 would be 17 characters wide

s is used for a string
f is for a floating point number

So for your heading, it would look like:
Java Code:
`System.out.printf("%#6s \t %#11s \t %#4s \t %#9s", "square", "square root", "cube", "cube root");`
Just do the same thing, with the same character width, for the numbers that you are outputting and everything will look perfect.

6. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,712
Rep Power
15

## Re: Why is the spacing so odd when I print this code?

# is used to decide the character width of that placement
No it isn't.

There is good documentation for how formatting works in the API docs of the Formatter class. printf() can be used to implement either of the approaches described by KevinWorkman. If you get stuck, post what you've tried and say which approach you are following (making the widths of printed elements the same or adjusting the amount of white space etc.)

#### Posting Permissions

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