, , , , ,

Formatted Output – Basics of Java Programming

Formatted Output

This subsection is not on any Java Developer Exam. It is solely included because many examples in this book format their output to aid in understanding the computed results.

For more control over how the values are printed, we can format the output. The following method of the java.io.PrintStream class can be used for this purpose:

Click here to view code image

PrintStream printf(String format, Object… args)

The String parameter format specifies how formatting will be done. It contains format specifications that determine how each subsequent value in the parameter args will be formatted and printed. The parameter declaration Object… args represents an array of zero or more arguments to be formatted and printed. The resulting string from the formatting will be printed to the destination stream. (System.out will print to the standard out object.)

Any error in the format string will result in a runtime exception.

This method returns the PrintStream on which the method is invoked, and can be ignored, as in the examples here.

The following call to the printf() method on the standard out object formats and prints three values:

Click here to view code image

System.out.printf(“Formatted values|%5d|%8.3f|%5s|%n”, // Format string
                   2016, Math.PI, “Hi”);               // Values to format

At runtime, the following line is printed in the terminal window:

Click here to view code image

Formatted values| 2016|  3.142|   Hi|

The format string is the first argument in the method call. It contains four format specifiers. The first three are %5d, %8.3f, and %5s, which specify how the three arguments should be processed. The letter in the format specifier indicates the type of value to format. Their location in the format string specifies where the text representation of the arguments should be inserted. The fourth format specifier, %n, is a platform-specific line separator. Its occurrence causes the current line to be terminated, with the cursor moving to the start of the next line. All other text in the format string is fixed, including any other spaces or punctuation, and is printed verbatim.

In the preceding example, the first value is formatted according to the first format specifier, the second value is formatted according to the second format specifier, and so on. The | character has been used in the format string to show how many character positions are taken up by the text representation of each value. The output shows that the int value was written right-justified, spanning five character positions using the format specifier %5d; the double value of Math.PI took up eight character positions and was rounded to three decimal places using the format specifier %8.3f; and the String value was written right-justified, spanning five character positions using the format specifier %5s. The format specifier %n terminates the current line. All other characters in the format string are printed verbatim.

Table 1.2 shows examples of some selected format specifiers that can be used to format values.

Table 1.2 Format Specifier Examples

Parameter valueFormat specExample valueString printedDescription
Integer value“%d”123“123”Occupies as many character positions as needed.
 “%6d”123” 123″Occupies six character positions and is right-justified. The printed string is padded with leading spaces, if necessary.
Floating-point value“%f”4.567“4.567000”Occupies as many character positions as needed, but always includes six decimal places.
 “%.2f”4.567“4.57”Occupies as many character positions as needed, but includes only two decimal places. The value is rounded in the output, if necessary.
 “%6.2f”4.567” 4.57″Occupies six character positions, including the decimal point, and uses two decimal places. The value is rounded in the output, if necessary.
Any object“%s”“Hi!”“Hi!”The text representation of the object occupies as many character positions as needed.
 “%6s”“Hi!”” Hi!”The text representation of the object occupies six character positions and is right-justified.
 “%-6s”“Hi!”“Hi! “The text representation of the object occupies six character positions and is left-justified.
Related Posts