1

How do I parse a text value into integer in Java ?

I have tried below 2 things;

Integer.valueOf("22,959");
Integer.parseInt("22,959");

But I am getting NumberFormatException with both.

Just to add, the text is dynamic, but would be in the comma format that i have given as example.

1
  • One option would be to strip the string of comma. Commented Sep 24, 2014 at 8:02

8 Answers 8

5

Use the NumberFormat class

    NumberFormat format = NumberFormat.getInstance();
    format.parse(num).intValue();

This default instance will be as per your default Locale. You can get instance according to the Locale by

    NumberFormat format = NumberFormat.getInstance(Locale.UK);
Sign up to request clarification or add additional context in comments.

Comments

1

Both the options are correct. valueOf returns a new Integer() object, while parseInt returns a primitive int.

However, your problem is with the comma, not with the method itself. So just do a

String value = "22,959";
value.replace(",", "");
Integer.valueOf(value);
Integer.parseInt(value);

Comments

1

if you have data with separators, you could use NumberFormat with locale

ie NumberFormat.getNumberInstance(Locale.US).parse("22,959")

Comments

0

Integer as mentioned in this name, only accept integer numbers. Means only number without dot o decimal separator. for removing decimal separator you can use replace or replaceAll methods for your string:

Integer.parseInt("22,959".replaceAll(",",""));

Comments

0

You need to strip the comma first :

Integer.valueOf("22,959".replaceAll(",", ""));

Comments

0

I suggest that you remove the comma with

Integer.parseInt("22,959".replaceAll(",", ""));

This assumes that you are definitely not going to ever have to deal with European locales, like Portuguese where they use decimal for number separation and comma for fractions.

if you want to cope with alternative locales, you'll need to parse the String using Numberformat:

To format a number for the current Locale, use one of the factory class methods:

myString = NumberFormat.getInstance().format(myNumber);

To format a number for a different Locale, specify it in the call to getInstance.

NumberFormat nf = NumberFormat.getInstance(Locale.FRENCH);

Then you just:

myNumber = nf.parse(myString);

Comments

-1

You're trying to parse float number, not Integer. Integer number should not have any decimal separator. Should look like Integer.parseInt("22"); For your case should look like: float f = Float.parseFloat("22,959");

1 Comment

It's probably an integer with comma as thousand separator.
-1

I agree with @primitivwurzel, Integer cannot have decimal. Use Double.parseDouble()/Double.valueOf() instead of Integer.parseInt()/Integer.valueOf()

plus, Decimal point separated by dot (.), not comma (,)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.