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

Thread: How to align text?

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    7
    My Mood
    Depressed
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Unhappy How to align text?

    How should I align text to the right?

    Here is a part from my code:

    System.out.println (subj1 + "\t" + gradesubj1 + "%"); // Subject and Mark for Subject 1

    I want to align the output for subj1 to the left.
    I want to align the output for gradesubj1 and the rest to the left.

    How should I do it?


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: How to align text?

    PrintStream (like System.out) also has a printf() method.

     
    import java.util.Map;
    import java.util.TreeMap;
     
    public class PrintfEg {
        public static void main(String[] args) {
            Map<String,Double> data = new TreeMap<String,Double>() {
                {
                    put("First", 2000.0);
                    put("Second", Math.PI);
                    put("Last", -1.0);
                }
            };
     
            for(Map.Entry<String,Double> entry :data.entrySet()) {
                System.out.printf("%-10s%8.3f%%%n", entry.getKey(), entry.getValue());
            }
        }
    }

    The format string, "%-10s%8.3f%%%n" determines how the output will appear. %-10s prints a left aligned string of width 10 and padded with spaces. %8.3f produces a floating point value, right aligned 8 characters wide with 3 decimal places. (Math.PI is suitably rounded). %% produces a % sign. And %n produces the newline character for the OS on which it runs.

    There is much more to the use of these format strings with detail linked to from the API docs.

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

    shifat96 (February 18th, 2012)

  4. #3
    Junior Member
    Join Date
    Feb 2012
    Posts
    7
    My Mood
    Depressed
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: How to align text?

    Thank You!

  5. #4
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: How to align text?

    You're welcome.

    There is probably too much detail in the format string syntax to try and remember. I tend to look things up as I need them. It's useful, though, because a similar formatting notation is used in a number of programming languages.

  6. #5
    Junior Member keep smiling's Avatar
    Join Date
    Oct 2011
    Location
    Italy
    Posts
    14
    My Mood
    Cool
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: How to align text?

    i got the same problem but i found help from this:

    Java - printf - YouTube

    i hope it helps you too

Similar Threads

  1. Can't seem to align the numbers
    By Wilkinsonr in forum What's Wrong With My Code?
    Replies: 5
    Last Post: January 29th, 2012, 05:37 PM
  2. GridBagLayout problems with align
    By lugaru in forum AWT / Java Swing
    Replies: 2
    Last Post: December 14th, 2011, 11:51 PM
  3. How do you align?
    By JavaStudent1988 in forum Java Theory & Questions
    Replies: 5
    Last Post: October 18th, 2011, 05:33 PM
  4. How to align the items in a form (grid layout)?
    By onlybarca in forum AWT / Java Swing
    Replies: 4
    Last Post: November 27th, 2010, 11:38 PM
  5. System.out.print -align
    By juwan in forum Object Oriented Programming
    Replies: 3
    Last Post: November 17th, 2010, 04:36 PM