数値に 0 を埋め込むと「IllegalFormatConversionException: d != java.lang.String」が発生しますか? 質問する

数値に 0 を埋め込むと「IllegalFormatConversionException: d != java.lang.String」が発生しますか? 質問する

昨日、次のような完全に動作するコードがありました。

int lastRecord = 1;
String key = String.format("%08d", Integer.toString(lastRecord));

これにより、00000001 にうまく埋め込まれます。

今度は、twoKeyChar でテーブルから文字列を取得し、lastRecord でテーブルから int を取得するようにして、さらにレベルを上げました。

ご覧のとおり、概念は基本的に同じです。int を文字列に変換し、0 で埋めようとしますが、今回は次のエラーが発生します。

java.util.IllegalFormatConversionException: d != java.lang.String

コードは以下の通りです:

String newPK = null;
String twoCharKey = getTwoCharKey(tablename);
if (twoCharKey != null) {
     int lastRecord = getLastRecord(tablename);
     lastRecord++;
     //The println below outputs the correct values: "RU" and 11. 
     System.out.println("twocharkey:"+twoCharKey+"record:"+lastRecord+"<");
     //Now just to make it RU00000011
     newPK = String.format("%08d", Integer.toString(lastRecord));
     newPK = twoCharKey.concat(newPK);
}

前回正常に動作していたときから、動作が停止する理由はないので、何か間違ったことを入力したに違いないと思います。 どのような助言やヒントでもいただければ幸いです。 ありがとうございます!

ベストアンサー1

以下は必要ありませんInteger.toString():

 newPK = String.format("%08d", lastRecord);

String.format()変換とパディングを行います。

おすすめ記事