Java NumberFormatException
我正在尝试将字符串解析为整数。我正在从excel文件中读取此字符串Java NumberFormatException,java,jxl,Java,Jxl,我正在尝试将字符串解析为整数。我正在从excel文件中读取此字符串 String nos=new String((sheet.getCell(1, i).getContents().replace(" NOS", ""))).trim().replaceAll("^ *", ""); int stock=Integer.parseInt(nos); 以下是错误java.lang.NumberFormatException:对于输入字符串:“827”您可以使用类似“\”*
String nos=new String((sheet.getCell(1, i).getContents().replace(" NOS", ""))).trim().replaceAll("^ *", "");
int stock=Integer.parseInt(nos);
以下是错误
java.lang.NumberFormatException:对于输入字符串:“827”
您可以使用类似“\”*(\\d+\“**”
的正则表达式,它可以选择匹配数字周围的引号和后面的任何内容。通过使用括号,我们对数字进行分组,然后我们可以像
String str = "\"\"827\"\" NOS";
Pattern p = Pattern.compile("\"*(\\d+)\"*.*");
Matcher m = p.matcher(str);
if (m.matches()) {
System.out.println(Integer.parseInt(m.group(1)));
}
输出为
827
添加另一个.replaceAll(“\”,”)