Results 1 to 6 of 6
  1. #1
    357mag is offline Member
    Join Date
    May 2010
    Posts
    36
    Rep Power
    0

    Default I'm trying to right justify my column of integers

    I've got a simple program that I want to practice and learn how to right justify the output. The output of my four arithmetic computations is simply four integers. I want the output to be right justified so it looks like this:

    30
    5
    -10
    200

    I've read about how to use the printf() method and about format specifiers. I figure all I need is a field width of 3 since the largest number is 200 and that occupies three spaces. But my program is not right justifying.

    My output looks more like this;

    30
    5
    -10
    200

    The 5 especially is not being printed correctly. I played with different numbers and I got the whole output to right justify but I had to change the field width of each output statement to a different number. I could not use the same number for each output statement. All the examples I have seen with other people show they are setting each output statement to the same field width and it works fine.

    The only thing I can surmise is that for instance the 2 in 200 is wider than the negative sign in -10 and because of differences like this I can't use the same number as the field width in each output statement. What is going wrong in my code?

    Here is the code:

    public class FormattingOutput
    {
    public static void main(String args[])
    {

    int a, b, c, d;

    a = 10 + 20;
    b = 10 - 5;
    c = 10 - 20;
    d = 10 * 20;

    System.out.printf("%3d\n", a);
    System.out.printf("%3d\n", b);
    System.out.printf("%3d\n", c);
    System.out.printf("%3d", d);
    }
    }

    Can you teach me how to use the code tags also?

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,209
    Rep Power
    12

    Default Re: I'm trying to right justify my column of integers

    By default, printf right justifies within the field. The above code works for me. How are you running it? Are you using an IDE?

    The easiest way to use code tags is to go into the advanced editor of your text, highlight the code, and then click on the # sign.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,209
    Rep Power
    12

    Default Re: I'm trying to right justify my column of integers

    Note, the right justification won't work unless you're using a fixed width font. Try Courier New or similar font.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    357mag is offline Member
    Join Date
    May 2010
    Posts
    36
    Rep Power
    0

    Default Re: I'm trying to right justify my column of integers

    I'm using Eclipse and the font I'm using is Cambria 12 pt.

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,209
    Rep Power
    12

    Default Re: I'm trying to right justify my column of integers

    That won't work. It's a variable font so padding on the left won't work. Try Courier New (what I use) or some other fixed width font.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    357mag is offline Member
    Join Date
    May 2010
    Posts
    36
    Rep Power
    0

    Default Re: I'm trying to right justify my column of integers

    Thank You So Much. Now I know.

Similar Threads

  1. How can I do to justify a text in a paragraph?
    By s.errani in forum Apache POI
    Replies: 11
    Last Post: 04-18-2017, 12:15 PM
  2. Replies: 0
    Last Post: 01-17-2013, 12:43 PM
  3. How to left justify and wrap text
    By Tejas in forum New To Java
    Replies: 12
    Last Post: 10-05-2011, 12:27 AM
  4. Replies: 4
    Last Post: 02-05-2011, 06:47 PM
  5. Help with Format.justify method
    By lenny in forum AWT / Swing
    Replies: 1
    Last Post: 08-07-2007, 06:29 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •