Java字符串格式化错误:IllegalFormatConversionException原因

洼地云 tuoyidashi.png

背景:想对字符串进行格式化,前面补零操作,代码如下:

if (!StringUtils.equals("0", areaCode.getCity()) && StringUtils.equals("0", areaCode.getArea()) && StringUtils.equals("0", areaCode.getTown())) {
    // 市级
    BeanUtils.copyProperties(areaCode, tourAreaInfo);
    tourAreaInfo.setLevel("2");
    tourAreaInfo.setPcode(String.format("%06d", areaCode.getProvince()));
}

结果报错:

Exception in thread "main" java.util.IllegalFormatConversionException: d != java.lang.String
    at java.util.Formatter$FormatSpecifier.failConversion(Formatter.java:4302)
    at java.util.Formatter$FormatSpecifier.printInteger(Formatter.java:2793)
    at java.util.Formatter$FormatSpecifier.print(Formatter.java:2747)
    at java.util.Formatter.format(Formatter.java:2520)
    at java.util.Formatter.format(Formatter.java:2455)
    at java.lang.String.format(String.java:2940)
    at com.portrail.city.area.model.AreaCode.convert2JwTourAreaInfo(AreaCode.java:33)
    at com.portrail.city.area.service.JwTourAreaInfoService.lambda$initAreaCode$1(JwTourAreaInfoService.java:53)
    at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
    at java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1382)
    at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
    at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
    at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)
    at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
    at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499)
    at com.portrail.city.area.service.JwTourAreaInfoService.initAreaCode(JwTourAreaInfoService.java:53)
    at com.portrail.city.area.service.JwTourAreaInfoService.main(JwTourAreaInfoService.java:66)

出错原因,d表示对整型进行转换格式,传入的却是字符串。

实现方式改为: Java字符串右侧补零实现

赞(1)
未经允许禁止转载:优米格 » Java字符串格式化错误:IllegalFormatConversionException原因

评论 抢沙发

合作&反馈&投稿

商务合作、问题反馈、投稿,欢迎联系

广告合作侵权联系

登录

找回密码

注册