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

Thread: Java using printf to format.

  1. #1
    Member
    Join Date
    Dec 2012
    Posts
    127
    My Mood
    Angelic
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Java using printf to format.

    Is it possible to print out the months like the picture below with that little space infront of January with printf? If I put "%10s" it will also make the months space far apart, I want before January to have a 10 blank space and between each months there 3 blank space. I try putting a "System.out.print(" ");" before the for loop but I'm trying to use printf . Thanks

    wadwdwa.png


    String month[]={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
            for(String monthOf : month)
            {
                System.out.printf("%6s",monthOf);
            }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java using printf to format.

    Can you build a line showing what you want to print?
    Use *s for spaces.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Dec 2012
    Posts
    127
    My Mood
    Angelic
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: Java using printf to format.

    **********S(Jan)***S(Feb)***S(Mar)***S(Apr)***S(Ma y)

    ^ Like that until Dec but disregard the ()

    The picture should give a clearer explaination.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java using printf to format.

    Print the first n spaces(FFFF) before the loop and then the desired spacing(SSS) in the loop
    FFFFFSSSJanSSSFeb...
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Dec 2012
    Posts
    127
    My Mood
    Angelic
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: Java using printf to format.

    Something like this?
            System.out.print("        ");
            for(String monthOf : month)
            {
                System.out.printf("%6s",monthOf);
            }

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java using printf to format.

    Did you try it? Did it give you what you want?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Dec 2012
    Posts
    127
    My Mood
    Angelic
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: Java using printf to format.

    Yes, I got it now. Thank you for your help! Happy New Year

Similar Threads

  1. [SOLVED] printf error
    By Thor in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 10th, 2012, 10:05 PM
  2. [SOLVED] Setting field width System.out.printf
    By ranjithfs1 in forum Java Theory & Questions
    Replies: 3
    Last Post: March 18th, 2012, 01:55 PM
  3. How to align text? Printf?
    By shifat96 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 20th, 2012, 12:51 PM
  4. about printf please help having errors
    By Macgrubber in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 15th, 2010, 11:01 PM
  5. The printf() method explanation needed
    By darek9576 in forum Object Oriented Programming
    Replies: 1
    Last Post: March 14th, 2010, 12:11 AM