Aligned Table
Completed
Use printf to print a two-row table: Tea costing 12.5 and Coffee costing 99. Names are left-aligned in 8 characters, prices right-aligned in 9 characters with 2 decimals.
Input
None.
Output
Two lines (see the example).
Example
Output
Tea 12.50 Coffee 99.00
Editor
Java
Hints
%-8s left-aligns a string in 8 characters.
%9.2f right-aligns a number in 9 characters.
Solution explanation
A minus sign in a format means left-align; the number is the minimum width.
Show solution
public class Main {
public static void main(String[] args) {
System.out.printf("%-8s%9.2f%n", "Tea", 12.5);
System.out.printf("%-8s%9.2f%n", "Coffee", 99.0);
}
}