昨天我有一个完美的工作代码,其形式如下:
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);
}
我觉得我一定是打错了,因为自从上次它工作以来,它没有理由崩溃。任何帮助/提示表示赞赏!谢谢你!
原文由 Metal Wing 发布,翻译遵循 CC BY-SA 4.0 许可协议
你不需要
Integer.toString()
:String.format()
将进行转换和填充。