背景:想对字符串进行格式化,前面补零操作,代码如下:
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字符串右侧补零实现。
最新评论
接口密码是什么呀?
牛仔
一直在用的,感谢更新
哎。之前听推荐的,但是现在要付费了,否则硬解等都受到限制。