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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s