将字符串转换为整形时: 代码如下(会报错): String count = "";
int result = Integer.parseInt(count);
System.out.println("转换结果为"+result); 执行结果(错误日志): Exception in thread "main" java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Integer.parseInt(Integer.java:470)
at java.lang.Integer.parseInt(Integer.java:499)
at com.paic.icore.pts.claim.biz.service.impl.Test.b(Test.java:39)
at com.paic.icore.pts.claim.biz.service.impl.Test.main(Test.java:9) 代码如下(会报错): String count = null;
int result = Integer.parseInt(count);
System.out.println("转换结果为"+result); 执行结果(错误日志): Exception in thread "main" java.lang.NumberFormatException: null
at java.lang.Integer.parseInt(Integer.java:417)
at java.lang.Integer.parseInt(Integer.java:499)
at com.paic.icore.pts.claim.biz.service.impl.Test.b(Test.java:39)
at com.paic.icore.pts.claim.biz.service.impl.Test.main(Test.java:9) 解决代码: String count = "4";
if (count == null || count.equals("") ) {
System.out.println("count为空");
} else {
int result = Integer.parseInt(count);
System.out.println("转换结果为"+result);
}
结果: 转换结果为4