// Formatting.java (c) 2005 Kari Laitinen // http://www.naturalprogramming.com // 2004-03-07 File created. // 2005-06-01 Last modification. // This program shows how so-called format specifiers can be // used when method System.out.printf() is called. // To find examples about formatting floating-point values, // see programs Miles.java and Distance.java. import java.util.* ; class Formatting { public static void main( String[] not_in_use ) { int some_integer = 123456 ; System.out.print( "\n 12345678901234567890 \n" ) ; System.out.printf( "\n %9d right justified", some_integer ) ; System.out.printf( "\n %-9d left justified", some_integer ) ; System.out.printf( "\n %9X right hexadecimal", some_integer ) ; System.out.printf( "\n %-9X left hexadecimal", some_integer ) ; System.out.printf( "\n %d no printing field", some_integer ) ; System.out.printf( "\n %X hexadecimal uppercase", some_integer ) ; System.out.printf( "\n %x hexadecimal lowercase", some_integer ) ; System.out.printf( "\n %012d leading zeroes", some_integer ) ; System.out.printf( "\n %012X hexadecimal", some_integer ) ; System.out.printf( "\n %,d digit grouping", some_integer ) ; System.out.print( "\n" ) ; Formatter text_to_print = new Formatter() ; text_to_print.format( "\n %9d right justified", some_integer ) ; System.out.print( text_to_print ) ; String another_text_to_print = String.format( "\n %09d leading zeroes", some_integer ) ; System.out.print( another_text_to_print ) ; another_text_to_print = "SOME TEXT" ; System.out.printf( "\n %s is a string.", another_text_to_print ) ; System.out.printf( "\n %c is a character.", 'k' ) ; } }