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: How to use printf statements with formatting symbols to align the labels and data

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    11
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default How to use printf statements with formatting symbols to align the labels and data

    Ok so I got my program to work (WOW) now my teacher wants me to use printf statements with formatting symbols to align the labels and data to make the outputs all pretty and organized. So the question is how? Is there a certain symbol that you use or what cause I don't remember one. Thanks in advance and I'll double check in my book in case I missed something easy.

    He wants it like this. (Also can someone remind me how to show it as code)

    Enter Price: $ 500
    ____________Discount: 15%
    ______Discounted Price: $ 425.00
    ____________Sales Tax: $ 37.19
    ________________Total: $ 462.19

    What I have is this and not sure what to do to make it organized.

    System.out.printf("Total: $ %,.2f" , total);


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: How to use printf statements with formatting symbols to align the labels and data

    Read the API for the util.Formatter class. It has extensive details of how it works. Note the printf method probably uses a Formatter object internally. Once you have read it all. Read it again and then try writing some test code to see what happens. If you get stuck come back, post your code and ask a specific question.
    Improving the world one idiot at a time!

  3. #3
    Member Kewish's Avatar
    Join Date
    Apr 2013
    Location
    Australia
    Posts
    116
    Thanks
    10
    Thanked 17 Times in 14 Posts

    Default Re: How to use printf statements with formatting symbols to align the labels and data

    Formatter API

    I used to have a nice little diagram for printf that I used to help others. Problem is that I uploaded it with Puush and that's only temporary. This reference should get you by.
    A printf format reference page

    Pay attention to the padding sections. Also, you may want to pass in all strings as arguments. That gives you more control over the output.

    Good luck.

  4. #4
    Member Kewish's Avatar
    Join Date
    Apr 2013
    Location
    Australia
    Posts
    116
    Thanks
    10
    Thanked 17 Times in 14 Posts

    Default

    Found it.

    http://puu.sh/2voOI.jpg <- printf padding

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. Java cant find symbols?
    By jjd712 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 19th, 2012, 10:33 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. Where are all the symbols!? Poor Java can't find them... :(
    By coffecupcake in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 17th, 2012, 03:08 PM
  5. 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