Reputation: 129
I was trying to convert the String
to int
. However, its encounter error throw a NumberFormatException
.
java.lang.NumberFormatException: For input string: "07221255201"
Code :
int value = Integer.valueOf(itemData.get(row).ERP_Customer_Item.trim());
Kindly advise.
Upvotes: 1
Views: 1127
Reputation: 9886
Your number is larger than Integer.MAX_VALUE (2^31 - 1, or 2147483647), so it can't be parsed into an int.
Be careful converting from strings to numbers. Whatever created that number wasn't using java ints.
You can use a Long.parseLong to convert to a long, or Long.valueOf to get a Long (object instead of primative) in this case, but why are you converting from string in the first place? You're losing information (in this case the leading zeros which may or may not be important depending on what you do with the data). Conversion has a way of biting back somewhere down the line.
Upvotes: 1
Reputation: 25950
7221255201 is out of primitive int
range. You should use long
type.
This should work fine:
long value = Long.parseLong(itemData.get(row).ERP_Customer_Item.trim());
And for other contexts, try to have a practice of keeping primitive type ranges in mind when you write code. It is like having a hammer set consisting of hammers of different size and using the appropriate one in the context. For example, if you know that a particular variable will always be in the range of [-128,127] you should use short
instead of int
since you will waste a lot of bits in the latter case.
Upvotes: 3