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

Thread: Printf Left Justify?

  1. #1
    Junior Member
    Join Date
    Sep 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post Printf Left Justify?

    Hey guys, just signed up. I'm currently at school in a CSI 1001 class and we're learning printf with JGrasp as the environment at the moment. I've looked all over the internet for help but they all have the same answer.

    Heres the problem.

    I have a string as this:

    String header1 = new String ("Caswell Catering and Convention Service");

    as for the printf line, its this:

    System.out.printf ("%-5s/n", header 1);

    The output is right justified no matter the use of the "-" or without and I can't seem to find the correct thing to do to make it left justified. Let me know what I'm doing wrong. If you guys need my whole code for this, please let me know, I'm pretty new to java and this forum in general so take it easy on me ;p.


  2. #2
    Member Ada Lovelace's Avatar
    Join Date
    May 2014
    Location
    South England UK
    Posts
    414
    My Mood
    Angelic
    Thanks
    27
    Thanked 61 Times in 55 Posts

    Default Re: Printf Left Justify?

    When you say left-justified are you talking about the text printing up against the left
    margin of the screen? I compiled your sample code and that is where my output was.

    If it is appearing like this:

               This is an output sample

    Then it could be an option in your IDE that needs setting. I've never used
    JGrasp myself so I cannot help you there. Take a look at the help files
    and documentation.

    Wishes Ada xx
    If to Err is human - then programmers are most human of us all.
    "The Analytical Engine offers a new, a vast, and a powerful language . . .
    for the purposes of mankind
    ."
    Augusta Ada Byron, Lady Lovelace (1851)

  3. #3
    Junior Member
    Join Date
    Sep 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Printf Left Justify?

    Hey Ada, thanks for the response. Yes, I'm talking about the text print from the left margin. IDE are you using? I'd love to try different ones.

    My output that I'm getting is this:

    Caswell Catering and Convention Service
    Final Bill
    -------------------------------------------

    As you can see, the text is not giving the proper output I want. If you could provide me some advice, I'd be grateful. Thanks!

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Printf Left Justify?

    the proper output I want.
    Can you post an example of what you want? Be sure to wrap it in code tags to preserve its formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Sep 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Printf Left Justify?

    Screen Shot 2014-09-07 at 6.14.20 PM.jpg

    This is an example given to me by my instructor. This is the output I'm trying to achieve. And can you tell me how to code tag? I'm not too far into java as of yet.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Printf Left Justify?

    That looks right justified: The end of the Strings are against the right side of the area
    When printing:
    The normal placing of Strings is left justified
    numbers are right justified.

    A way I've found to right justify Strings is to pad them with leading spaces to push them right to the desired place.
    |left adjusted      |
    |     right adjusted|
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Sep 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Printf Left Justify?

    Leading spaces does work, I just tried it. But what I'm trying to do is make a string left justified and make a double right justified.

    like for instance:

    String example1 = "This is a test";

    double example2;
    ----
    System.out.print("Your input was: ");
    example2 = kb.nextDouble();
    ----
    System.out.printf ("%-1s %2f\n", example 1, example 2)


    and I want a output something like:


    (1 space)Your input value was: (next.Double)(2 spaces from left)
    (1 space)>another string: (another double)(2 spaces from left)
    (1 space)>repeat: (repeat)(2 spaces from left)

    This is what I'm doing in repeat
    Screen Shot 2014-09-07 at 10.22.32 PM.jpg

    This is what I have so far. Everything on the printf to the left after the spacing is string and to the right is a double.

    Screen Shot 2014-09-07 at 10.30.22 PM.jpg
    This is my output. Maybe this can explain whats going on?
    Attached Images Attached Images
    Last edited by Sumdude; September 7th, 2014 at 10:31 PM.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Printf Left Justify?

    what I'm trying to do is make a string left justified and make a double right justified.
    Please copy and post the output here and wrap it in code tags:

    [code=java]
    YOUR OUTPUT GOES HERE
    [/code]
    to get highlighting and preserve formatting.

          System.out.printf ("|%-1s| |%2f|\n", "example 1", 1.2);  
     //|example 1| |1.200000|
    The width's are too small to show adjustment. Enlarge them:
          System.out.printf ("|%-20s| |%20f|\n", "example 1", 1.2);  
     // |example 1           | |            1.200000|
    Above the String is left adjusted and the number is right adjusted.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] Need help with printf
    By Elyril in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 24th, 2014, 03:40 PM
  2. Text Formatting/left, right justify
    By Melac in forum What's Wrong With My Code?
    Replies: 6
    Last Post: December 12th, 2013, 03:50 AM
  3. Rounding with printf
    By Brandos12 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 23rd, 2013, 07:22 PM
  4. [SOLVED] printf error
    By Thor in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 10th, 2012, 10:05 PM
  5. justify output
    By _lithium_ in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 28th, 2011, 10:21 PM