Java Console format() Example
Console class format() method example. This example shows you how to use format() method.
Syntax is : public Console format(String fmt, Object... args)
Method writes a formatted string to this console's output stream using the specified format string and arguments.
Here is the code.
/**
* @(#) FormatConsole.java
* A class representing use of method format() of Console class in
java.io Package.
* @Version 11-June-2008
* @author Rose India Team
*/
import java.io.*;
public class FormatConsole {
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";
// format method call.
objConsole.format(formatString, "Id", "A", "B");
objConsole.format(formatString, "-----", "-----", "-----");
objConsole.format(formatString, "11", "10", "100");
objConsole.format(formatString, "12", "20", "200");
objConsole.format(formatString, "13", "30", "300");
objConsole.format(formatString, "14", "40", "400");
}
}
} |
Output of the program.
Id A B
----- ----- -----
11 10 100
12 20 200
13 30 300
14 40 400 |
|