TestPrintf.java
Select all
/* TestPrintf.java David Klick CIS 260 2005-01-19 Demonstrates formatted output using java.util.Formatter and the printf method of PrintStream. */ import java.util.Formatter; public class TestPrintf { public static void main(String[] args) { Formatter f = new Formatter(); // separate steps f.format("Hello, world!"); System.out.println(f); // like above, but two steps combined in each line // notice how results keep getting appended to last results System.out.println(f.format("%5d**", 9)); System.out.println(f.format("%7.2f**", 9.123)); System.out.println(f.format("%-5d**", 9)); System.out.println(f.format("%05d**", 9)); System.out.println(f.format("%05o**", 9)); // skipping overt use of Formatter and just using printf // notice how old results are cleared whenever we do a new conversion System.out.printf("%5x**\n", 42); System.out.printf("%5X**\n", 42); System.out.printf("Hi %s\n", "Dave"); System.out.printf("%c\n", (char)75); System.out.printf("%d\n", (int)'d'); System.out.printf("9 < 5 is %b\n", (9<5)); System.out.printf("%2$d %d %d %d\n", 1, 2, 3, 4); } } /* Output from program: Hello, world! Hello, world! 9** Hello, world! 9** 9.12** Hello, world! 9** 9.12**9 ** Hello, world! 9** 9.12**9 **00009** Hello, world! 9** 9.12**9 **00009**00011** 2a** 2A** Hi Dave K 100 9 < 5 is false 2 1 2 3 */