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

Thread: Output in columns

  1. #1
    Member melki0795's Avatar
    Join Date
    Nov 2013
    Location
    Malta
    Posts
    49
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Output in columns

    Hi,

    Any way i can format this output into columns?

    for(int i = 0; i < 5; i++) {
        System.out.println(t[i]+n[i]+s[i]);
     }

    Best Regards


  2. #2
    Member
    Join Date
    Mar 2012
    Location
    United States
    Posts
    118
    My Mood
    Inspired
    Thanks
    1
    Thanked 33 Times in 31 Posts

    Default Re: Output in columns

    Look into System.out.prinf().

  3. The Following User Says Thank You to KucerakJM For This Useful Post:

    GregBrannon (March 1st, 2014)

  4. #3
    Member melki0795's Avatar
    Join Date
    Nov 2013
    Location
    Malta
    Posts
    49
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Output in columns

    Just outputs all messed up o.O

  5. #4
    Member
    Join Date
    Mar 2012
    Location
    United States
    Posts
    118
    My Mood
    Inspired
    Thanks
    1
    Thanked 33 Times in 31 Posts

    Default Re: Output in columns

    Did you happen to look at any documenation before trying to use printf? It isn't used exactly like the other print commands so System.out.printf(t[i]+n[i]+s[i]) would definitely not give you what you are looking for. For that matter, not posting what you used in your code won't help to get an answer to the issue. However, if you look for some info on printf I'm sure you can work it out.

    NOTE: System.out.format() can do the same thing.
    Last edited by KucerakJM; March 1st, 2014 at 02:06 PM.

  6. The Following User Says Thank You to KucerakJM For This Useful Post:

    GregBrannon (March 1st, 2014)

  7. #5
    Member melki0795's Avatar
    Join Date
    Nov 2013
    Location
    Malta
    Posts
    49
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Output in columns

    I did, but when I use them, all i get is the t[i] written in a line :/

    for(int i = 0; i < 5; i++) {
                System.out.printf(title[i],"%20s", name[i], "%20s",subAmounts[i]);
           }

  8. #6
    Member
    Join Date
    Mar 2012
    Location
    United States
    Posts
    118
    My Mood
    Inspired
    Thanks
    1
    Thanked 33 Times in 31 Posts

    Default Re: Output in columns

    Alright, that clears some things up. That isn't quite the correct way to use printf()'s formatting capabilities. The basic syntax would be:
    System.out.printf("format string", data1, data2, data3.....);
    And this is what you are trying
    System.out.printf(data1, "format string", data2, "format string", data3);
    It should be noted that all formating information will be contained in the first argument for printf.

  9. The Following User Says Thank You to KucerakJM For This Useful Post:

    GregBrannon (March 1st, 2014)

  10. #7
    Member melki0795's Avatar
    Join Date
    Nov 2013
    Location
    Malta
    Posts
    49
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Output in columns

    and would this work, if t[i] are Strings of different lengths? What is happening, is that my output is shifting here and there.

    Capture.PNG

    This is the Format string i used: String formatString = " %-15s %20s %20.1f %n";

  11. #8
    Member
    Join Date
    Mar 2012
    Location
    United States
    Posts
    118
    My Mood
    Inspired
    Thanks
    1
    Thanked 33 Times in 31 Posts

    Default Re: Output in columns

    Your format string, as it stands, creates a 15 character left justified column(type string); a 20 character right justified column (type string); then another 20 character right justified column (type float); last a new line (basically). If there are different lengths then you want to make the column big enough to accomodate the largest length, plus some for a bit of padding if the next column is also left justified which is probably what you want.

  12. The Following 2 Users Say Thank You to KucerakJM For This Useful Post:

    GregBrannon (March 1st, 2014), melki0795 (March 1st, 2014)

  13. #9
    Member melki0795's Avatar
    Join Date
    Nov 2013
    Location
    Malta
    Posts
    49
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Output in columns

    thanks man, I got it, I always used to find formatting a bitch heh.

    Thanks

Similar Threads

  1. Replies: 1
    Last Post: February 5th, 2014, 09:22 AM
  2. help with printing output in columns
    By jblankinship in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 14th, 2012, 01:03 PM
  3. JList with multiple columns
    By aussiemcgr in forum Java Theory & Questions
    Replies: 2
    Last Post: January 27th, 2011, 08:44 AM
  4. [SOLVED] Tabbing columns
    By SnarkKnuckle in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 24th, 2011, 10:50 PM
  5. Need a loop for rows and columns
    By Ceasar in forum Loops & Control Statements
    Replies: 8
    Last Post: October 9th, 2009, 05:47 PM

Tags for this Thread