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: String formatting alignment

  1. #1
    Member
    Join Date
    May 2013
    Posts
    165
    Thanks
    58
    Thanked 1 Time in 1 Post

    Default String formatting alignment

    I am trying format marry string so they align in the same way e.g.

    This is how I would like them to be formatted:
      Cricket                $100
      Football               $100
      Basketball             $1
    The total Cost is        $201

    This is the method I have tried though I get mix results when different inputs are given, sometimes it works sometimes it doesn't.

    This is what it mostly ends up like:
     Cricket                 $100
     Football               $100
     Basketball                 $1
    The total Cost is        $201

    I have tried experimenting by having different right-align in code for total cost though nearly always get wayward results, though it has worked a couple of times on different values of variable length where right-align and left-align were different.
    This is the code I have tried:
    System.out.printf("%-15s %15s %n", " Cricket", "$100");
    System.out.printf("%-15s %15s %n", " Football", "$100");
    System.out.printf("%-15s %15s %n", " Basketball", "$1");
    System.out.printf("%-15s %15s %n", "The total Cost is", "$201");

    I am not bothered about how much spacing there is in between, I just want to align the $ signs as I have managed to align the sports. Where am I going wrong?


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

    Default Re: String formatting alignment

    Quote Originally Posted by keepStriving View Post
    This is how I would like them to be formatted:
      Cricket                $100
      Football               $100
      Basketball             $1
    The total Cost is        $201
    This is a solution
    System.out.printf("%-24s %s %n", " Cricket", "$100");
    System.out.printf("%-24s %s %n", " Football", "$100");
    System.out.printf("%-24s %s %n", " Basketball", "$1");
    System.out.printf("%-24s %s %n", "The total Cost is", "$201");
    Note the %-24s (just to be sure that "The total Cost is" does not exceed) and that the second specification is simply %s (so left aligned as you want).
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

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

    keepStriving (December 4th, 2013)

  4. #3
    Forum VIP
    Join Date
    Jun 2011
    Posts
    317
    My Mood
    Bored
    Thanks
    47
    Thanked 89 Times in 74 Posts
    Blog Entries
    4

    Default Re: String formatting alignment

    I would use StringBuilder's insert(int offset, String str) to force a column. I don't use printf much so I can't say what is going on.

    public void printToColumn(String item, String cost) {
        int itemColumn = 1;
        int priceColumn = 15;
     
        StringBuilder b = new StringBuilder();
        b.append("                                    ");  
        b.insert(itemColumn, item);
        b.insert(priceColumn, cost);
        System.out.println(b.toString());
    }

  5. The Following User Says Thank You to ChristopherLowe For This Useful Post:

    keepStriving (December 4th, 2013)

  6. #4
    Member
    Join Date
    May 2013
    Posts
    165
    Thanks
    58
    Thanked 1 Time in 1 Post

    Default Re: String formatting alignment

    Thanks guys, I was wondering what made you choose -24% of all numbers you could use?
    I got printf to work for terminal window which is what I wanted though not for JTextArea which still prints it wayward, I'll try String Builder for JTextArea.

  7. #5
    Forum VIP
    Join Date
    Jun 2011
    Posts
    317
    My Mood
    Bored
    Thanks
    47
    Thanked 89 Times in 74 Posts
    Blog Entries
    4

    Default Re: String formatting alignment

    You didn't mention a JTextArea. Font spacing and kerning are important in this situation so use a Monospace font like Courier. See SWT - OS agnostic way to get monospaced font for a platform independent solution.

  8. #6
    Member
    Join Date
    May 2013
    Posts
    165
    Thanks
    58
    Thanked 1 Time in 1 Post

    Default Re: String formatting alignment

    I am passing the correct alignment to JTextArea as I've done a print verification though JTextArea is still not aligning correctly, is this a trait of JText Area?

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

    Default Re: String formatting alignment

    Quote Originally Posted by keepStriving View Post
    not for JTextArea which still prints it wayward
    Text alignment in a text component that uses a variable-width font is not easy (and StringBuffer/StringBuilder don't help much since the problem is not about character units but pixel units, depending on the font and single characters).
    Something is possible using tabs ('\t') and see also the documentation of setTabSize of JTextArea. However, if you can use a fixed-width font in JTextArea it's really better.
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

  10. #8
    Member
    Join Date
    May 2013
    Posts
    165
    Thanks
    58
    Thanked 1 Time in 1 Post

    Default Re: String formatting alignment

    This code sorted the problem.
    new Font(Font.MONOSPACED, Font.PLAIN, 12);

Similar Threads

  1. String formatting
    By aussiemcgr in forum Java Theory & Questions
    Replies: 5
    Last Post: September 24th, 2013, 10:55 AM
  2. Output alignment help
    By rossonomous in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 18th, 2011, 04:40 PM
  3. String Formatting on JOptionPane.showMessageDialog
    By Onur in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 23rd, 2011, 08:39 AM
  4. Alignment issues
    By fride360 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: March 10th, 2011, 02:58 PM
  5. Alignment
    By angelaffxmaniac in forum Java Theory & Questions
    Replies: 1
    Last Post: May 13th, 2010, 08:55 AM