“IllegalFormatConversionException:d!= java.lang.String”当填充数字为0时? [英] "IllegalFormatConversionException: d != java.lang.String" when padding number with 0s?

查看:1956
本文介绍了“IllegalFormatConversionException:d!= java.lang.String”当填充数字为0时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我昨天有一个完美的代码,其确切形式为:

I had a perfectly working code yesterday, in the exact form of:

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

哪个会很好地填充到00000001。

Which would pad it nicely to 00000001.

现在我把它踢了一个缺口,两个KeyChar从表中获取一个字符串,lastRecord从一个表中获取一个int。

Now I kicked it up a notch with twoKeyChar getting a string from a table and lastRecord getting an int from a table.

正如你所看到的,这个概念本质上是相同 - 我将int转换为字符串并尝试用0填充它;但是,这次我收到以下错误:

As you can see the concept is essentially the same - I convert an int to a string and try to pad it with 0s; however, this time I get the following error:

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);
}

我觉得我必须输错了,因为没有理由它自上次工作以来就打破了。任何帮助/提示表示赞赏!谢谢!

I feel like I must have typed something wrong, because there is no reason for it to break since the last time when it worked. Any help/hint is appreciated! Thank You!

推荐答案

您不需要 Integer.toString()

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

String.format()将执行转换和填充。

这篇关于“IllegalFormatConversionException:d!= java.lang.String”当填充数字为0时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆