Java Console printf() Example
Console class printf() method example. This example shows you how to use printf() method.
Syntax is : public Console printf(String format, Object... args)
This is a convenience method to write a formatted string to this console's output stream using the specified format string and arguments.
Here is the code.
/**
* @(#) PrintfConsole.java
* A class representing use of method printf() of Console class in
java.io Package.
* @Version 11-June-2008
* @author Rose India Team
*/
import java.io.*;
public class PrintfConsole {
public static void main(String[] args) {
//Create a console object.
Console objConsole = System.console();
if (objConsole != null) {
String formatString = "%1$4s %2$10s %3$10s%n";
// printf method call.
objConsole.printf(formatString, "Id", "A", "B");
objConsole.printf(formatString, "-----", "-----", "-----");
objConsole.printf(formatString, "11", "10", "100");
objConsole.printf(formatString, "12", "20", "200");
objConsole.printf(formatString, "13", "30", "300");
objConsole.printf(formatString, "14", "40", "400");
}
}
} |
Output of the program.
Id A B
----- ----- -----
11 10 100
12 20 200
13 30 300
14 40 400 |
|