背景:想对字符串进行格式化,前面补零操作,代码如下:
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字符串右侧补零实现。
最新评论
不错挺好用的,东至梅城何红涛林场的 何红涛2个月前 (10-18)回复
识别码不对
呵呵
我的是ipv4网络,如何使用直播源啊!