[英]Integer.parseInt does not parse String to int
下面的代码来自一个试图从提交的html表单中读取数据的Servlet。 变量fieldValue
是一个String并打印正确的值(如此BizStr: 5
)但是当我尝试将此值解析为整数时,它不会打印任何内容。
for(FileItem uploadItem : uploadItems){
if(uploadItem.isFormField()){
String fieldName = uploadItem.getFieldName();
String fieldValue = uploadItem.getString();
if(fieldName.equals("business_id")){
out.println("BizStr: "+ fieldValue +"\n");
out.println("BizInt: "+ Integer.parseInt(fieldValue )+"\n");
}
}
}
为什么这个字符串没有被解析为整数?
测试:
Integer.parseInt(" 5"); // space before; yields NumberFormatException
Integer.parseInt("5 "); // space after; yields NumberFormatException
在解析之前尝试使用fieldValue
上的trim()
:
out.println("BizInt: "+ Integer.parseInt(fieldValue.trim() )+"\n");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.