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

Thread: Printf syntax woes

  1. #1
    Member
    Join Date
    Sep 2013
    Posts
    64
    Thanks
    24
    Thanked 0 Times in 0 Posts

    Default Printf syntax woes

    I have this line of code:

    System.out.printf("%8.1f", solutions[i][j]);

    Where solutions is a two-dimensional array. This is within a nested for loop, but don't worry about all of that. This line of code is successfully printing the value of "solutions", rounding to 1 decimal, and aligning each result to the far right of 8 spaces.

    I want to align it in the center, not the far right.

    So I looked at this site:

    Complete Printf for Java Format String Specification

    Which says I can use the "^" flag to center the result in the given specified width.

    And that I should use this format: %[flags][width][.precision][argsize]typechar

    So I wrote this code:

    System.out.printf("%^8.1f", solutions[i][j]);

    Which compiles, but creates a run time error.

    This is the error:

    HTML Code:
    Exception in thread "main" java.util.UnknownFormatConversionException: Conversion = '^'
            at java.util.Formatter.checkText(Formatter.java:2547)
            at java.util.Formatter.parse(Formatter.java:2533)
            at java.util.Formatter.format(Formatter.java:2469)
            at java.io.PrintStream.format(PrintStream.java:970)
            at java.io.PrintStream.printf(PrintStream.java:871)
            at HW1.printTable(HW1.java:95)
            at HW1.main(HW1.java:23)


    What am I doing wrong?

    Thanks in advance.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,066
    My Mood
    Mellow
    Thanks
    184
    Thanked 652 Times in 639 Posts

    Default Re: Printf syntax woes

    I believe the documentation you're referring to (and provided a link to) is for a 3rd party add-on or library. The official Java Formatter documentation is here.

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

    ineedahero (September 21st, 2013)

  4. #3
    Member
    Join Date
    Sep 2013
    Posts
    64
    Thanks
    24
    Thanked 0 Times in 0 Posts

    Default Re: Printf syntax woes

    Okay, I figured out how to make it left-aligned and it actually looks like how I want it to be. Couldn't find anything on center-alignment...it seems really esoteric. But it doesn't matter- left-alignment looks much nicer than I thought.

    Thank you a lot for the help...I never would have known.

Similar Threads

  1. Java printf
    By maple1100 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 27th, 2013, 03:20 PM
  2. Java using printf to format.
    By maple1100 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: December 31st, 2012, 07:29 PM
  3. [SOLVED] printf error
    By Thor in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 10th, 2012, 10:05 PM
  4. [SOLVED] Dialog Woes...
    By snowguy13 in forum AWT / Java Swing
    Replies: 1
    Last Post: February 25th, 2012, 08: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, 10:01 PM