Java 1.5/5

Intro to printf

 

Java has some elegant, object-orientated ways to generate very nice layouts--requiring many steps.

 

The following concession to a C-programming technique is ugly but more direct--and similar to fomatting techniques in many other languages. 

 

Basic sysntax for use:

     System.out.printf( formatting-string, arguments-to-print );

This is the basic form of the formatting-string:

     %[flags][width][.precision]conversion

 

Strings:

String greeting = "Hello";

System.out.printf( "%10s", greeting );

            displays 5 spaces followed by Hello for a total of 10 columns (right aligned).

System.out.printf( "%-10s", greeting );

            displays Hello followed by 5 spaces for a total of 10 columns (left aligned)

System.out.printf( "%s", greeting );

            displays Hello for a total of 5 columns

System.out.printf( "%2s", greeting );

            displays Hello -- does not truncate

 

Integers

int num = 327;

System.out.printf( "%8d", num );  // this d means base 10

            right-aligns 327 in a total of 8 columns (5 spaces of left padding)

System.out.printf( "%-8d", num );

            left-aligns 327 in a total of 8 columns (5 spaces of right padding)

System.out.printf( "%d", num );

            displays 327 in 3 spaces

System.out.printf( "%1d", num );

            displays 327 in 3 spaces (numbers are not truncated!)

 

Doubles (note the automatic rounding!)

double num = 234.567;

System.out.printf( "%8.2f", num );    // this d means base 10

            right-aligns 234.57 in a total of 8 columns (2 spaces of left padding)

System.out.printf( "%-8.2f", num );

            left-aligns 234.57 in a total of 8 columns (2 spaces of right padding)

System.out.printf( "%f", num );

            displays 234.567000 -- the default display for floating point numbers

System.out.printf( "%4.3f", num );

            displays 234.567 in 7 spaces -- the integer part is never truncated

 

Printf does not generate a newline.  You can use as many statements like the previous examples as you need to generate 1 full line.  But you will have to do something else to force the output to a new line!

 

You can also put several specs into 1 formatting string, followed by as many arguments as there are %-conversions in the formatting string.  You can put literal text inside the formatting-string.   

 

Literal text will be included literally.  A % is the signal to printf to begin converting and substituting the next argument.  Printf is then on the look-out for a conversion symbol (such as s, d or f).  That's how printf knows it has read all of your specification for 1 argument.  You can include some special characters in the formatting-string.  Remember that the fomatting string is used by printf, not println -- the rules are different.

 

%n  for newline             ( \n for println )

%t  for tab                   ( \t for println )          

 

System.out.printf( "ID:  %5d\tName:  %s%n", num, greeting );

generates this and moves on to the next line

ID:   327 Name:  Hello

 

There are MANY more options.  Google "Java printf" for more examples -- when you are ready.