45

I'm getting NumberFormatException when I try to parse 265,858 with Integer.parseInt().

Is there any way to parse it into an integer?

9
  • 5
    the question is not clean, what is ','? thousands or decimal delimiter? Commented Aug 15, 2012 at 16:44
  • 5
    I think it is... Is there any way to parse it into an integer? Commented Aug 15, 2012 at 16:46
  • 1
    or is it 2 integers in a csv format? 265 and 858? Commented Aug 15, 2012 at 16:47
  • 5
    "an integer" is not 2 integers. Commented Aug 15, 2012 at 16:48
  • 2
    @vivek_jonam, the original title was confusing before I edited it. In computer science, the term "comma separated" has a special meaning. When people read "comma separated", they expect to be dealing with a list of values separated by commas. Not a single value that uses commas for groupings. Commented Aug 15, 2012 at 17:05

7 Answers 7

86

Is this comma a decimal separator or are these two numbers? In the first case you must provide Locale to NumberFormat class that uses comma as decimal separator:

NumberFormat.getNumberInstance(Locale.FRANCE).parse("265,858")

This results in 265.858. But using US locale you'll get 265858:

NumberFormat.getNumberInstance(java.util.Locale.US).parse("265,858")

That's because in France they treat comma as decimal separator while in US - as grouping (thousand) separator.

If these are two numbers - String.split() them and parse two separate strings independently.

Sign up to request clarification or add additional context in comments.

6 Comments

"parse it into an integer" pretty much narrows down the interpretation :)
Third option: it's a grouping separator.
Yes: one integer, thousands separator.
@MarkoTopolnik: thanks, I clarified it and added more examples. Indeed OP wants to parse integers - I missed that.
unfortunatly, if we input "265-858" it will output "265" which is not desired. is there a way to make it more strict so it throws an exception?
|
22

You can remove the , before parsing it to an int:

int i = Integer.parseInt(myNumberString.replaceAll(",", ""));

1 Comment

Take note that replaceAll is used, critical for any number >999,999. This also removes the need to throw/catch the checked exception from other answers.
13

If it is one number & you want to remove separators, NumberFormat will return a number to you. Just make sure to use the correct Locale when using the getNumberInstance method.

For instance, some Locales swap the comma and decimal point to what you may be used to.

Then just use the intValue method to return an integer. You'll have to wrap the whole thing in a try/catch block though, to account for Parse Exceptions.

try {
    NumberFormat ukFormat = NumberFormat.getNumberInstance(Locale.UK);
    ukFormat.parse("265,858").intValue();
} catch(ParseException e) {
    //Handle exception
}

Comments

5

One option would be to strip the commas:

"265,858".replaceAll(",","");

1 Comment

Simpler and faster solution: replace instead of replaceAll.
4

The first thing which clicks to me, assuming this is a single number, is...

String number = "265,858";
number.replaceAll(",","");
Integer num = Integer.parseInt(number);

Comments

2

Or you could use NumberFormat.parse, setting it to be integer only.

http://docs.oracle.com/javase/1.4.2/docs/api/java/text/NumberFormat.html#parse(java.lang.String)

Comments

0

Try this:

String x = "265,858 ";
    x = x.split(",")[0];
    System.out.println(Integer.parseInt(x));

EDIT : if you want it rounded to the nearest Integer :

    String x = "265,858 ";
    x = x.replaceAll(",",".");
    System.out.println(Math.round(Double.parseDouble(x)));

5 Comments

That should give 265, he doesn't want it rounded or what so ever
But the number is 265858 or 265.858
If you read OP question, you don't know if the number must be 265 or 265858. Even if it were 265, maybe OP needs it as 266.
This question has nothing to do with rounding, floating point or decimal numbers. Comma is quite simply a thousands separator and all the treatment it needs is ignoring.
Come on, well he just can use the replaceAll part, I've just shown something as an example, say some tweaks or whatever !

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.