0

I am reading high volume of records from a csv file. one of the column is amount and it has 2 decimal places. So I wanted to parse it to integer form but it hit error when come to this as below :

int trxnAmt = Integer.parseInt("002428859600");

Suppose it will be 2428859600 right ?

but it throws me error > java.lang.NumberFormatException: For input string: "002428859600"

I tried to use :

long a = Long.parseLong("002428859600");

it worked fine for me .

I still cannot find out what's going on. Is the number too big ?

1
  • Integer.MAX_VALUE = 0x7fffffff - your number is 90c574d0, so yes: It's too big for an Integer. Commented Mar 11, 2013 at 12:13

5 Answers 5

4

Yes 2428859600 is a long number. try to assign it a int and you'd get a compiler error :

int i = 2428859600;  // error as 2428859600 is clearly outta int range(2,147,483,647)
long l = 2428859600L; //no error
Sign up to request clarification or add additional context in comments.

Comments

2

Yes, you are passing out of the range integer value to parseInt method.

The int data type is a 32-bit signed two's complement integer. It has a minimum value of -2,147,483,648 and a maximum value of 2,147,483,647 (inclusive).

Try long or other suitable options.
This might help you : http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

Comments

1

Is the number too big?

Yes. An int is 4 bytes, so the biggest number that will fit into an int is 24*8-1-1 = 231-1 = 2147483647 (4*8-1 because 1 bit is required for sign).

See this.

Comments

-1

You should make sure that your input really is an integer (i.e. "16"), or if it's out of Integer range, then use Double.parseDouble() or Long.parseLong()

Comments

-1

You are getting the java.lang.NumberFormatException: For input string: "002428859600" because, the number 002428859600 cannot be represented as a 32-bit integer (type int). It can be represented as a 64-bit integer (type long). long literals in Java end with an "L": 002428859600L

4 Comments

That long literals end with an "L" does not have anything to do with the problem. The number in the string is not a number literal.
I did not mean that you can try Integer.parseInt("002428859600L"); to resolve the problem. I just mentioned the root cause of the exception. If you will get the same exception if you try with Integer.parseInt("002428859600L"); You have misinterpreted I guess...
The first sentence of your answer is correct; 2428859600 doesn't fit in a 32-bit signed int. But why do you mention that long literals end with an "L"? That's also true, but is totally irrelevant for the problem.
@SarathKumarSivan Note that L is not a valid character for parseInt, Integer.parseInt("123L") throws a NumberFormatException.

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.