19

昨日、完全に機能するコードがありました。正確な形式は次のとおりです。

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

これにより、00000001 にうまくパディングされます。

ここで、テーブルから文字列を取得する twoKeyChar と、テーブルから int を取得する lastRecord を使用して、ワンランク上に上げました。

ご覧のとおり、概念は本質的に同じです。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);
}

最後に機能したときから壊れる理由がないので、何か間違って入力したに違いない気がします。どんな助け/ヒントも大歓迎です! ありがとう!

4

4 に答える 4

31

あなたは必要ありませんInteger.toString()

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

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

于 2012-06-05T19:24:44.587 に答える