Output Formatting

 

Earlier we learned how to show the output of a program by using System.out and the methods print and println. However, these methods cannot directly format certain outputs in a specific manner. For example, the default output of floating-point numbers is typically up to 6 decimal places for float values and up to 15 decimal places for double values. Moreover, sometimes we would like to align the output in certain columns. Java 1.5 introduced the ability to output data formatted according to a string specification provided by the programmer. This ability is provided by the method printf.

The syntax to use the method printf to produce output on the standard output device (console) is:

     System.out.printf(formatString);

or

     System.out.printf(formatString, argumentList);

Where formatString is a string specifying the format of the output and argumentList is a list of arguments. The argumentList is a list of arguments that consists of constant values, variables, or expressions. If argumentList has more than one argument, then the arguments are separated by commas.

For example,

     System.out.printf("Hi Mom!");

consists of only the formatString and

     System.out.printf("The value of the constant literal is %d\n",123);

consists of both the formatString and the argumentList.

Format String Syntax

A formatString can contain fixed text along with embedded format specifiers. The character % indicates the start of a format specifier. The syntax for format specifiers is:

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

Conversion

The required conversion is a character that specifies how the parameter should be formatted. These are the conversion characters we will be using this semester:

  Conversion The result is
's' general a string
'c' character a single character
'd' integral formatted as an integer
'f' floating point formatted as a real number
'%' percent %

Flags

The optional flags is a set of characters that modifies the output format. The set of valid flags depends on the conversion. For example, the flag – specifies that the result should be left-justified. It can be applied to all the parameter categories. The flag ^ specifies that the result should be converted to uppercase. This flag applies to any parameter category that can result in alphabetic characters being produced. The flag + specifies that the result will include a sign. A space character for a flag specifies that the result will include a leading space for positive values. The flag 0 specifies that the result be zero padded. The flag ( specifies that negative numbers should be enclosed in parentheses.

Width

The optional width is a (decimal) integer indicating the minimum number of characters to be written to the output. For the line separator conversion, including a width is invalid. If the converted value results in fewer than width characters, then padding characters (often blanks) are used to pad the converted value to the specified width.

In a format specifier, if the number of columns in the option width is less than the number of columns required to output the value of the expression, the output is expanded to the required number of columns. That is the output is not truncated. For example, the output of the statement:

     System.out.printf("%2d", 8756);

is

     8756

even though only two columns are specified to output 8756, which requires four columns.

Precision

The optional precision is a (decimal) integer usually used to restrict the number of characters. For f conversions, precision is the number of digits to the right of the decimal point. For character, integral, and percent conversions, including precision is invalid.

Example:

     num = 96;
     rate = 15.50;

     System.out.println("123456789012345");
     System.out.printf("%5d \n", num);
     System.out.printf("%5.2f \n", rate);
     System.out printf("%5d%6.2f \n", num,rate);
     System.out.printf("%5d %6.2f \n",num,rate);

Output

     123456789012345
        96
     15.50
        96 15.50
        96  15.50

Example:

     int num = 763;
     double x = 658.75;
     String str = "Java Program.";

     System.out.println("123456789012345678901234567890”);
     System.out.printf("%5d%7.2f%15s\n", num, x, str);
     System.out.printf("%15s%6d%9.2f\n", str,num,x);
     System.out.printf("%8.2f%7d%15s\n",x,num,str);
     System.out.printf("num = %5d\n",num);
     System.out.printf("x = %10.2f\n", x);
     System.out.printf("str = %15s\n",str);
     System.out.printf("%6s%7d\n", "Example", 1);

Output

     123456789012345678901234567890
       763 658.75  Java Program.
       Java Program.   763   658.75
       658.75    763  Java Program.

     num =   763
     x =     658.75
     str =   Java Program.
     Example      1

Email Me | Office Hours | My Home Page | Department Home | MCC Home Page

© Copyright Emmi Schatz 2014