Java NumberFormatException

116 views Asked by At

I am trying to parse a string to Integer. Im reading this string from a excel File.

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

Here is the Errorjava.lang.NumberFormatException: For input string: """827"""

1

There are 1 answers

0
Elliott Frisch On

You could use a regular expression like "\"*(\\d+)\"*.*" which will optionally match quotes around digits and anything following. By using the parenthesis we are grouping the digits and then we can get that group out like

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)));
}

Output is

827