Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By pbrockway2

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

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

    Default 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. Why is the spacing so odd when I print this code?-table-output.png

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,038
    Rep Power
    10

    Default 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.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

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

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

    How would I counteract this?

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,038
    Rep Power
    10

    Default 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.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

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

    Default 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. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default 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.)
    KevinWorkman likes this.

Similar Threads

  1. Need to find a way to print line spacing by words
    By technoreject in forum New To Java
    Replies: 1
    Last Post: 07-07-2011, 03:43 PM
  2. [Q] Spacing Help
    By iriscience in forum New To Java
    Replies: 7
    Last Post: 02-08-2011, 02:44 AM
  3. Code Print Error! Help needed
    By socboy6579 in forum New To Java
    Replies: 13
    Last Post: 11-16-2010, 04:35 AM
  4. How to use and print the JAVA code from a JSP
    By Ginkan in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 02-23-2010, 03:05 PM
  5. Replies: 11
    Last Post: 04-16-2008, 02:52 PM

Posting Permissions

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