Code /* IOapalooza8.java CIS 260 David Klick 2006-01-18 This program demonstrates output formatting using the new System.out.printf() method and the associated Formatter class. */ import java.util.Formatter; import java.util.Calendar; import java.util.Date; public class IOapalooza8 { public static void main(String[] args) { int n = 123; double x = 123.456789; boolean b = true; String s = "Hello, world!"; char c1 = 'a', c2 = 'b', c3 = 'c'; Date date = new Date(); // demonstration of printf method System.out.printf("*\n"); System.out.printf("n = %d\n", n); // decimal integer System.out.printf("n = %5d\n", n); // 5 positions System.out.printf("n = %05d\n", n); // 0 fill on left System.out.printf("n = %5o (octal)\n", n); // octal, 5 wide System.out.printf("n = %05o (octal)\n", n); // 0 fill on left System.out.printf("n = %#5o (octal)\n", n); // print leading 0 System.out.printf("n = %5x (hex)\n", n); // hex, 5 wide (lowercase) System.out.printf("n = %05X (hex)\n", n); // 0 fill on left (uppercase) System.out.printf("n = %#5X (hex)\n", n); // print leading 0X System.out.printf("n = %#5x (hex)\n", n); // print leading 0x System.out.printf("n = %-5X (hex)\n", n); // left justify System.out.printf("**\n"); System.out.printf("x = %e\n", x); // scientific notation System.out.printf("x = %E\n", x); // uppercase 'E' System.out.printf("x = %f\n", x); // floating-point System.out.printf("x = %G\n", x); // either E or f System.out.printf("x = %.2E\n", x); // 2 positions after decimal System.out.printf("x = %a\n", x); // output float as hex (lowercase) System.out.printf("x = %A\n", x); // output float as hex (uppercase) System.out.printf("x = %(f, -x = %(f\n", x, -x); // output negative in parens System.out.printf("%,d\n", 1234567); // add ',' as separator System.out.printf("***\n"); System.out.printf("%b %B\n", b, b); // boolean System.out.printf("%s %S %h %H\n", s, s, s, s); // strings (upper, lower, hashcodes) System.out.printf("%3$c %1$c %2$c %1$c \n", c1, c2, c3); // char and positional specifiers System.out.printf("****\n"); System.out.printf("tc: %tc\n", date); // full date format System.out.printf("tF: %tF\n", date); // yyyy-mm-dd System.out.printf("tD: %tD\n", date); // mm/dd/yy System.out.printf("tm: %tm\n", date); // month (01-31) System.out.printf("te: %te\n", date); // day (1-31) System.out.printf("td: %td\n", date); // day (01-31) System.out.printf("tY: %tY\n", date); // yyyy System.out.printf("ty: %ty\n", date); // yy System.out.printf("TC: %TC\n", date); // int(year/100) System.out.printf("tB: %tB\n", date); // long month name System.out.printf("tb: %tb\n", date); // short month name System.out.printf("tj: %tj\n", date); // Julian day of year System.out.printf("tA: %tA\n", date); // long day name System.out.printf("ta: %ta\n", date); // short day name System.out.printf("*****\n"); System.out.printf("tr: %tr\n", date); // hh:mm:ss ?M (12 hour format) System.out.printf("tR: %tR\n", date); // hh:mm (24 hour format) System.out.printf("tT: %tT\n", date); // hh:mm:ss (24 hour format) System.out.printf("tH: %tH\n", date); // hh (00-23) System.out.printf("tI: %tI\n", date); // hh (01-12) System.out.printf("tk: %tk\n", date); // hour (0-23) System.out.printf("tL: %tL\n", date); // milliseconds (000-999) System.out.printf("tN: %tN\n", date); // nanoseconds (000000000-999999999) System.out.printf("tl: %tl\n", date); // hour (1-12) System.out.printf("tM: %tM\n", date); // minute (00-59) System.out.printf("tS: %tS\n", date); // seconds (00-59) System.out.printf("ts: %ts\n", date); // seconds since 1/1/1970 System.out.printf("tQ: %tQ\n", date); // milliseconds since 1/1/1970 System.out.printf("tZ: %tZ\n", date); // time zone System.out.printf("tz: %tz\n", date); // offset from GMT System.out.printf("Tp: %Tp\n", date); // AM/PM System.out.printf("tp: %tp\n", date); // am/pm System.out.printf("*****\n"); // Formatter class Formatter fmt = new Formatter(); fmt.format("%2$3.2f %1$04d\n", n, x); System.out.print(fmt.toString()); // String class format method String sOut = String.format("%2$3.2f %1$04d\n", n, x); System.out.print(sOut); } }