Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Output Alignment

  1. #1
    Junior Member
    Join Date
    Aug 2010
    Location
    Malaysia
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Output Alignment

    hye guys,
    i'm newbie here so with java with programming.
    i did a addition table, follow the tutorial from webit.net. i guess all my coding is correct but the table isn't in straight lines.
    here's my coding. could someone help me?
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
     
    package tables;
     
    /**
     *
     * @author FahMi
     */
    public class Main {
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            //Print Header
                System.out.println("Addition Table:");
     
                for(int i = 0; i <= 10; i++)
                {
                    for(int j = 0; j <= 10; j++)
                    {
                        System.out.print( (j + i) + "\t" );
                    }
                    System.out.println("");
                }
     
        }
     
    }

    ---------
    output
    ---------
    run:
    Addition Table:
    0        1        2        3        4        5        6        7        8        9        10        
    1        2        3        4        5        6        7        8        9        10        11        
    2        3        4        5        6        7        8        9        10        11        12        
    3        4        5        6        7        8        9        10        11        12        13        
    4        5        6        7        8        9        10        11        12        13        14        
    5        6        7        8        9        10        11        12        13        14        15        
    6        7        8        9        10        11        12        13        14        15        16        
    7        8        9        10        11        12        13        14        15        16        17        
    8        9        10        11        12        13        14        15        16        17        18        
    9        10        11        12        13        14        15        16        17        18        19        
    10        11        12        13        14        15        16        17        18        19        20
    BUILD SUCCESSFUL (total time: 1 second)
    Last edited by helloworld922; August 22nd, 2010 at 09:24 AM.
    Wanna be in my web developer team???


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Output Alignment

    Please don't post "hijack" someone else's post please Start a new topic if you have a separate un-related question.

    Also, make sure you use [highlight] tags around your code, they make it much easier to read it. If you're unsure how to do this, see my signature.

    It's not aligning because you're printing out some 2-digit numbers, which take up more space than 1-digit numbers.

    To manually fix this, you can calculate how many spaces to print out based on the number of digits you're printing out. There are also "automatic formatters" built into Java, but they're a little more difficult to use.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,623
    Thanks
    45
    Thanked 1,958 Times in 1,932 Posts

    Default Re: Output Alignment

    Using tab characters gives you left alignment. Ie the first character of each string will be in the same column. I don't know why your output doesn't show that. When I run your code I get:
    0	1	2	3	4	5	6	7	8	9	10	
    1	2	3	4	5	6	7	8	9	10	11	
    2	3	4	5	6	7	8	9	10	11	12	
    3	4	5	6	7	8	9	10	11	12	13	
    4	5	6	7	8	9	10	11	12	13	14	
    5	6	7	8	9	10	11	12	13	14	15	
    6	7	8	9	10	11	12	13	14	15	16	
    7	8	9	10	11	12	13	14	15	16	17	
    8	9	10	11	12	13	14	15	16	17	18	
    9	10	11	12	13	14	15	16	17	18	19	
    10	11	12	13	14	15	16	17	18	19	20

    Notice that all the first digits are in column.

  4. #4
    Junior Member
    Join Date
    Aug 2010
    Location
    Malaysia
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Output Alignment

    Quote Originally Posted by helloworld922 View Post
    Please don't post "hijack" someone else's post please Start a new topic if you have a separate un-related question.

    Also, make sure you use [highlight] tags around your code, they make it much easier to read it. If you're unsure how to do this, see my signature.

    It's not aligning because you're printing out some 2-digit numbers, which take up more space than 1-digit numbers.

    To manually fix this, you can calculate how many spaces to print out based on the number of digits you're printing out. There are also "automatic formatters" built into Java, but they're a little more difficult to use.
    Ok next time i'll start a new thread. Could you elaborate more about the "automatic formatters"?

    Quote Originally Posted by Norm View Post
    Using tab characters gives you left alignment. Ie the first character of each string will be in the same column. I don't know why your output doesn't show that. When I run your code I get:
    0	1	2	3	4	5	6	7	8	9	10	
    1	2	3	4	5	6	7	8	9	10	11	
    2	3	4	5	6	7	8	9	10	11	12	
    3	4	5	6	7	8	9	10	11	12	13	
    4	5	6	7	8	9	10	11	12	13	14	
    5	6	7	8	9	10	11	12	13	14	15	
    6	7	8	9	10	11	12	13	14	15	16	
    7	8	9	10	11	12	13	14	15	16	17	
    8	9	10	11	12	13	14	15	16	17	18	
    9	10	11	12	13	14	15	16	17	18	19	
    10	11	12	13	14	15	16	17	18	19	20

    Notice that all the first digits are in column.
    Thanks for your info though
    Wanna be in my web developer team???

Similar Threads

  1. Output Alignment
    By xdrechsler in forum Loops & Control Statements
    Replies: 1
    Last Post: August 18th, 2010, 10:26 AM
  2. Why am I getting this output?
    By ptabatt in forum What's Wrong With My Code?
    Replies: 5
    Last Post: August 6th, 2010, 07:36 PM
  3. The positioning and alignment of the text on the paper to be printed
    By java_fledgeling in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: June 8th, 2010, 08:54 PM
  4. Alignment
    By angelaffxmaniac in forum Java Theory & Questions
    Replies: 1
    Last Post: May 13th, 2010, 08:55 AM
  5. Replies: 1
    Last Post: March 31st, 2009, 02:49 PM