-1

I'm trying to generate a table of output in Java with the second column values lined up exactly on top of each other. Instead I'm getting my value "pushed" off to the right, even though the String doesn't overlap the padding. I know it is a rather long String, but it's only 27 and the padding is specified as 30, so shouldn't it just fill in the last three spaces and print out there instead of jumping so far to the right?

I tried changing all of the padding values to 1 and the 30 to a 15 but they still do not line up. The price does moves over but doesn't line up with the other lines when I change their padding value to match.

I guess I could just make them different numbers, but I want this to align correctly with any size String, under 30 characters. Changing the length of the price from 17.46 to 0.0 doesn't seem to help either.

I have to cut it all the way down to "2 Medium Red Roaste" in order for it to work, at "2 Medium Red Roasted" it begins to shift over. So it's working correctly at 19 characters, but not at 20.

System.out.printf ("%d %-13s%30.2f%n",2,"Medium Roasted Red Pepper",17.46);
System.out.printf ("%d %-13s%30.2f%n",1, "Large Cheese" ,7.69);
System.out.printf ("%-13s %30.2f%n","Tax" , 1.76);
System.out.printf ("%-13s %30.2f%n","Delivery" , 2.69);
System.out.printf ("%-13s %30.2f%n","Total" , 29.61);

I keep getting this as output:

2 Medium Roasted Red Pepper___________________________17.46
1 Large Cheese___________________________7.69
Tax_____________________________________1.76
Delivery________________________________2.69
Total__________________________________29.61

I was expecting this:

2 Medium Roasted Red Pepper_____________17.46
1 Large Cheese__________________________7.69
Tax____________________________________1.76
Delivery________________________________2.69
Total__________________________________29.61

(Where all the numbers line up on the right)

Slaw
  • 37,820
  • 8
  • 53
  • 80
reflektor
  • 21
  • 8

1 Answers1

0

Your code does not align as you expect because a negative width means minimum width for that field. If the field is wider than the specified width, it will use the space it needs. (An ugly table is probably better than a misleading ones with truncated values.)

To get your alignment right, you'll need to adjust for the biggest item you want to display. You also cannot use the width for the price column for this, %30.2f means to always display (room for) 30 digits before the decimal point. You should make a reasonable guess for the column width here or check for the maximum length needed as well.

public class RightAligned {
    public static void main(String[] args){
        String[] items = new String[] {
            "some really long item that extends far to the right",
            "short item",
            "medium longish item",
        };

        int longest = 0;
        for (String item : items) {
            if (longest < item.length()) {
                longest = item.length();
            }
        }

        String formatString = "%-" + longest + "s %5.2f%n";
        for (String item : items) {
            System.out.printf(formatString, item, 123.45);
        }
    }
}
Robert
  • 7,394
  • 40
  • 45
  • 64