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)