crchin
crchin

Reputation: 9683

Integer.parseInt() throws "For input string: XXX"

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 ?

Upvotes: 0

Views: 1800

Answers (5)

1218985
1218985

Reputation: 8032

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

Upvotes: -1

Ajinkya
Ajinkya

Reputation: 22720

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

Upvotes: 2

Bernhard Barker
Bernhard Barker

Reputation: 55649

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.

Upvotes: 1

Jeevan Patil
Jeevan Patil

Reputation: 6089

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()

Upvotes: -1

PermGenError
PermGenError

Reputation: 46438

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

Upvotes: 4

Related Questions