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: Text Formatting/left, right justify

  1. #1
    Junior Member
    Join Date
    Dec 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Text Formatting/left, right justify

    Hi,

    I do not know how to resolve this, I used "-"flag but it does seem change anything.
    The point of a whole task is to left-justify a column and later to right-justify.
    Any ideas?
     
    public class TestStringFormatting {
     
        public static void main(String[] args) {
      String a ="my dog barks,my cat does not";
        String b ="yellow cushions, red carpet";
        String c ="window, chair, table";
        String d ="pillow, tea";
    //left justified
     
        System.out.printf("%-1s%n",a);
     
        System.out.printf("%-1s%n",b);
     
        System.out.printf("%-1s%n",c);
     
        System.out.printf("%-1s%n",d);
     
    //right justified
        System.out.printf("%1$1s%n",a);
     
        System.out.printf("%1s%n",b);
     
        System.out.printf("%1s%n",c);
     
        System.out.printf("%1s%n",d);
     
    }
    }

    Output:
    my dog barks,my cat does not
    yellow cushions, red carpet
    window, chair, table
    pillow, tea
    my dog barks,my cat does not
    yellow cushions, red carpet
    window, chair, table
    pillow, tea
    Press any key to continue . . .


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,613
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Text Formatting/left, right justify

    Can you post the program's output to show what the problem is?

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Text Formatting/left, right justify

    Quote Originally Posted by Melac View Post
    I used "-"flag but it does seem change anything.
    If you use just %-1s or %1s the width specification ('1') is too short to see the effect of left/right alignment.
    Try:

    public class LRAlignment {
        public static void main(String[] args) {
            System.out.printf("##%-20s##%n", "pillow, tea");
            System.out.printf("##%20s##%n" , "pillow, tea");
        }
    }
    The output is:
    ##pillow, tea         ##
    ##         pillow, tea##
    Andrea, www.andbin.net SCJP 5 (91%) SCWCD 5 (94%)

    Useful links for Java beginners My new project Java Examples on Google Code

  4. #4
    Junior Member
    Join Date
    Dec 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Text Formatting/left, right justify

    Thank you andbin!
    It looks like I missed that bit.It all works fine now.

    Grazie mille!

  5. #5
    Junior Member
    Join Date
    Dec 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Text Formatting/left, right justify

    I also realized that the width specification number should be greater than the total number of signs in a longest string-so that could have an effect.

  6. #6
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Text Formatting/left, right justify

    Quote Originally Posted by Melac View Post
    I also realized that the width specification number should be greater than the total number of signs in a longest string-so that could have an effect.
    Yes, from java.util.Formatter's javadoc:
    The optional width is a non-negative decimal integer indicating the minimum number of characters to be written to the output.
    Andrea, www.andbin.net SCJP 5 (91%) SCWCD 5 (94%)

    Useful links for Java beginners My new project Java Examples on Google Code

  7. #7
    Junior Member
    Join Date
    Dec 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Text Formatting/left, right justify

    Ok.It should be equal or greater.
    All good.

    Thanks a lot.

Similar Threads

  1. Aligning left for JMenuBar
    By Shzylo in forum AWT / Java Swing
    Replies: 1
    Last Post: September 30th, 2013, 08:25 AM
  2. Formatting a text file
    By ExtremophileElite in forum What's Wrong With My Code?
    Replies: 15
    Last Post: April 4th, 2013, 02:14 PM
  3. how to use the keyboard up,down,right,left
    By carmz in forum AWT / Java Swing
    Replies: 9
    Last Post: February 24th, 2013, 06:11 AM
  4. justify output
    By _lithium_ in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 28th, 2011, 09:21 PM