Java-io-console-format
提供:Dev Guides
Java.io.Console.format()メソッド
説明
- java.io.Console.format(String fmt、Object … args)*メソッドは、指定されたフォーマット文字列引数を使用して、このコンソールの出力ストリームにフォーマットされた文字列を書き込みます。
宣言
以下は* java.io.Console.format(String fmt、Object …の宣言です。 args)*メソッド-
public Console format(String fmt, Object... args)
パラメーター
- fmt -書式文字列の構文で説明されている書式文字列
- args -フォーマット文字列のフォーマット指定子によって参照される引数。
戻り値
このメソッドは、このコンソールを返します。
例外
*IllegalFormatException* -書式文字列に不正な構文、指定文字列と互換性のない書式指定子、書式文字列に指定された引数が不十分、またはその他の不正な条件が含まれる場合
例
次の例は、java.io.Console.format(String fmt、Object … args)メソッド。
package com.finddevguides;
import java.io.Console;
public class ConsoleDemo {
public static void main(String[] args) {
Console cnsl = null;
try {
cnsl = System.console();
if (cnsl != null) {
String fmt = "%1$4s %2$10s %3$10s%n";
//format
cnsl.format(fmt, "Items", "Quanity", "Price");
cnsl.format(fmt, "-----", "-----", "-----");
cnsl.format(fmt, "Tomato", "1Kg", "15");
cnsl.format(fmt, "Potato", "5Kg", "50");
cnsl.format(fmt, "Onion", "2Kg", "30");
cnsl.format(fmt, "Apple", "4Kg", "80");
}
} catch(Exception ex) {
//if any error occurs
ex.printStackTrace();
}
}
}
上記のプログラムをコンパイルして実行すると、次の結果が生成されます-
Items Quantity Price
----- -------- -----
Tomato 1Kg 15
Potato 5Kg 50
Onion 2Kg 30
Apple 4Kg 80