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

Thread: How do you align?

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    10
    My Mood
    Stressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How do you align?

    Hello, I wrote a program to generate the times tables. Now it works and everything but it is not aligned as in all the units, tens etc are not ordered. Basically, instead of:

    1 2 3 4 5 6 7 8 9
    2 4 6 8 10 12 14 16 18
    3 6 9 12 15 18 21 24 27
    4 8 12 16 20 24 28 32 36
    5 10 15 20 25 30 35 40 45
    6 12 18 24 30 36 42 48 54
    7 14 21 28 35 42 49 56 63
    8 16 24 32 40 48 56 64 72
    9 18 27 36 45 54 63 72 81

    I'm getting:

    1 2 3 4 5 6 7 8 9
    2 4 6 8 10 12 14 16 18
    3 6 9 12 15 18 21 24 27
    4 8 12 16 20 24 28 32 36
    5 10 15 20 25 30 35 40 45
    6 12 18 24 30 36 42 48 54
    7 14 21 28 35 42 49 56 63
    8 16 24 32 40 48 56 64 72
    9 18 27 36 45 54 63 72 81

    edit: damn it, it doesn't show here, but you get the gist I hope


  2. #2
    Member
    Join Date
    Oct 2011
    Posts
    42
    My Mood
    Sneaky
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: How do you align?

    Have a look at what "\t" does in a String(or System.out)

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    10
    My Mood
    Stressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How do you align?

    can you tell me where to look

  4. #4
    Member
    Join Date
    Oct 2011
    Posts
    42
    My Mood
    Sneaky
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: How do you align?

    In between numbers you will be printing a " ", have a look what happens if you print a "\t" instead

  5. #5
    Junior Member
    Join Date
    Oct 2011
    Posts
    10
    My Mood
    Stressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How do you align?

    Not quite, see attached for what i want

    this_is_what_I_want.jpg
    Quote Originally Posted by Tsarin View Post
    In between numbers you will be printing a " ", have a look what happens if you print a "\t" instead

  6. #6
    Member
    Join Date
    Oct 2011
    Posts
    42
    My Mood
    Sneaky
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: How do you align?

    The most simple way would be to have a set number of spaces between each number. remove spaces based on the number of characters (e.g. 27 is 2).

    Otherwise you can use the Formatter class, there is quite a good tutorial:
    Henrik Bengtsson's Java printf, sprintf, fprintf (scanf, sscanf and fprintf)

Similar Threads

  1. How to align the items in a form (grid layout)?
    By onlybarca in forum AWT / Java Swing
    Replies: 4
    Last Post: November 27th, 2010, 10:38 PM
  2. System.out.print -align
    By juwan in forum Object Oriented Programming
    Replies: 3
    Last Post: November 17th, 2010, 03:36 PM