... with Field Widths and Justification
Line 10 outputs the headers for two columns of output. The first column displays the year and the second column the amount on deposit at the end of that year. We use the format specifier %20s
to output the String “Amount on Deposit”
. The integer 20
between the %
and the conversion character s
indicates that the value should be displayed with a field width of 20—that is, printf
displays the value with at least 20 character positions. If the value to be output is less than 20 character positions wide (17 characters in this example), the value is right justified in the field by default. If the year
value to be output were more than four character positions wide, the field width would be extended to the right to accommodate ...
Get Java How To Program, Late Objects, 11th Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.