Met*_*ing 10 java regex string-formatting
昨天我有一个完美的代码,完全符合以下形式:
int lastRecord = 1;
String key = String.format("%08d", Integer.toString(lastRecord));
Run Code Online (Sandbox Code Playgroud)
哪个会很好地填充到00000001.
现在我把它踢了一个缺口,两个KeyChar从一个表中获取一个字符串,而lastRecord从一个表中获取一个int.
正如您所看到的,概念本质上是相同的 - 我将int转换为字符串并尝试用0填充它; 但是,这次我收到以下错误:
java.util.IllegalFormatConversionException: d != java.lang.String
Run Code Online (Sandbox Code Playgroud)
代码如下:
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);
}
Run Code Online (Sandbox Code Playgroud)
我觉得我必须输入错误的东西,因为自上次工作以来没有理由让它破裂.任何帮助/提示表示赞赏!谢谢!
NPE*_*NPE 20
你不需要Integer.toString()
:
newPK = String.format("%08d", lastRecord);
Run Code Online (Sandbox Code Playgroud)
String.format()
将进行转换和填充.
归档时间: |
|
查看次数: |
35456 次 |
最近记录: |