Java NumberFormatException

时间:2014-11-17 05:27:33

标签: java jxl

我正在尝试将字符串解析为Integer。我正在从excel文件中读取这个字符串。

String nos=new String((sheet.getCell(1, i).getContents().replace(" NOS", ""))).trim().replaceAll("^ *", "");
            int stock=Integer.parseInt(nos);

以下是错误java.lang.NumberFormatException: For input string: """827"""

1 个答案:

答案 0 :(得分:0)

您可以使用像"\"*(\\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