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

• 10-09-2012, 07:43 PM
HalfAZN
Why is the spacing so odd when I print this code?
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. Attachment 4149
• 10-09-2012, 07:57 PM
KevinWorkman
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.
• 10-09-2012, 07:58 PM
HalfAZN
Re: Why is the spacing so odd when I print this code?
How would I counteract this?
• 10-09-2012, 08:03 PM
KevinWorkman
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.
• 10-09-2012, 08:23 PM
penguinCoder
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:
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:
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.
• 10-10-2012, 12:02 AM
pbrockway2
Re: Why is the spacing so odd when I print this code?
Quote:

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