Java Programming
Console for input and output
Output with printf
Two methods of the System.out object
A syntax to use the method printf to produce output on the standard output device 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 that consists of
constant values, variables, or expressions.
If there is more than one argument in argumentList,
then the arguments are separated with commas.
Example:
System.out.printf("Hello there!"); consists of only the format string, and the statement: System.out.printf("There are %.2f inches in %d centimeters.%n", centimeters / 2.54, centimeters); consists of both the format string and argumentList
- %.2f and %d are called format specifiers
- By default, there is a one-to-one correspondence between format specifiers and the arguments in argumentList
- The first format specifier %.2f is matched with the first argument, which is the expression centimeters / 2.54
- The second format specifier %d is matched with the second argument, which is centimeters
- The format specifier %n positions the insertion point at the beginning of the next line.
A format specifier for general, character, and numeric types has the following syntax:
%[argument_index$][flags][width][.precision]conversion
- The expressions in square brackets are optional; they may or may not appear in a format specifier
- The option argument_index is a (decimal) integer indicating the position of the argument in the argument list
- The first argument is referenced by "1$", the second by "2$", etc.
- The option flags is a set of characters that modify the output format
- The option width is a (decimal) integer indicating the minimum number of characters to be written to the output
- The option precision is a (decimal) integer usually used to restrict the number of characters
- The required conversion is a character indicating how the argument should be formatted
Here are some of Java's supported conversions
's' | general | The result is a string |
'c' | character | The result is a Unicode character |
'd' | integral | The result is formatted as a (decimal) integer |
'e' | floating point | The result is formatted as a decimal number in computerized scientific notation |
'f' | floating point | The result is formatted as a decimal number |
'%' | percent | The result is '%' |
'n' | line separator | The result is the platform-specific line separator |
Code that formats the output of decimal numbers.
public class printf1 { public static void main(String[] args) { double x = 15.674; //Line 1 double y = 235.73; //Line 2 double z = 9525.9864; //Line 3 System.out.println("Line 4: The values of x, " + "y, and z with two decimal " + "places."); //Line 4 System.out.printf("Line 5: x = %.2f %n", x); //Line 5 System.out.printf("Line 6: y = %.2f %n", y); //Line 6 System.out.printf("Line 7: z = %.2f %n", z); //Line 7 System.out.println("Line 8: The values of x, " + "y, and z with three decimal " + "places."); //Line 8 System.out.printf("Line 9: x = %.3f %n", x); //Line 9 System.out.printf("Line 10: y = %.3f %n", y); //Line 10 System.out.printf("Line 11: z = %.3f %n", z); //Line 11 } }The console after the program finishes
Code that formats the output using the format specifier.
public class printf2 { public static void main(String[] args) { int num = 763; //Line 1 double x = 658.75; //Line 2 String str = "Java Program."; //Line 3 System.out.println("1234567890123456789" + "01234567890"); //Line 4 System.out.printf("%-5d%-7.2f%-15s ***%n", num, x, str); //Line 5 flag="-" to output left justified. System.out.printf("%-15s%-6d%-9.2f ***%n", str, num, x); //Line 6 System.out.printf("%-8.2f%-7d%-15s ***%n", x, num, str); //Line 7 System.out.printf("num = %-5d ***%n", num); //Line 8 System.out.printf("x = %-10.2f ***%n", x); //Line 9 System.out.printf("str = %-15s ***%n", str); //Line 10 System.out.printf("%-10s%-7d ***%n", "Program No.", 4); //Line 11 } }The console after the program finishes