/* TestPrintf.java CIS 160 David Klick 2015-09-02 Demonstration of formatted output. */ public class TestPrintf { public static void main(String[] args) { // Declare and initialize variables int n = 5; double x = 12.34567; String str = "Test"; // Formatted output // %-10s: s means String, 10 columns wide, left justified (pads on right with spaces) // %04d: d means integer value, 4 columns wide, right justified, pad on left with 0s // %6.3f: f means floating point value, 6 columns wide, 3 decimal positions // Note that the decimal point is counted as a column in the %f format // %n: newline System.out.printf("s=%-10s n=%04d x=%6.3f%nEnd of program%n", str, n, x); } } /* Displays: s=Test n=0005 x=12.346 End of program */