Daily Archives: June 4, 2010

Solving NumberFormatException


Often we tend to parse the integer value from a String object using Integer.valueOf(strValue) or Integer.parseInt(strValue). This will work as far as we pass the string like ‘1234567’ without any grouping.. The problme arises only if we have grouping on the string which is parsed..

Bump..

parseInt() and valueOf() method throws NumberFormatException for this.

So how to resolve this,

rewrite the code and make use of parse() method in java.text.NumberFormat class

String str = "123,456";
 int i = 0;
 try {
 i = NumberFormat.getNumberInstance().parse(str).intValue();
 } catch (ParseException e) {
 e.printStackTrace();
 }
 System.out.println(i);