Reputation: 351
I'm getting the following error when trying to parse the phone number, "5554567899"
java.lang.NumberFormatException: For input string: "5554567899"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:495)
at java.lang.Integer.parseInt(Integer.java:527)
...
Obviously, the phone number is able to be parsed. I even referenced this question, and the byte code result was
53,53,53,52,53,54,55,56,57,57,
So there's no invisible characters in the string. I'm stumped, can anyone help?
Here's the section of the code that's throwing the error, for reference:
String fullNumber = null;
for(Phone phone : party.getPhones()) {
if(phone != null && !phone.getDialNumber().isEmpty()) {
if(!phone.getAreaCode().isEmpty()) {
fullNumber = phone.getAreaCode();
}
fullNumber += phone.getDialNumber();
if(phone1 == null || phone1.equals(0)) {
LOGGER.debug(displayCharValues(fullNumber));
phone1 = Integer.parseInt(fullNumber);
}
}
phone1
is of Java.lang.Integer
type.
Upvotes: 1
Views: 2556
Reputation: 159854
5554567899
exceeds Integer.MAX_VALUE
(2147483647). Use Long
or BigInteger
instead:
BigInteger bigNumber = new BigInteger("5554567899");
Upvotes: 1
Reputation: 41145
Really a phone number shouldn't be treated as a number at all, but as a string or possibly as a datatype that you define yourself, breaking out the country code, area code, exchange, etc.
If you want to validate that it contains only numbers or numbers plus some of the standard phone-number separators, this can be done by regular expressions.
We call it a phone number, and it is a numeric string, but as the usage isn't for numeric computation, you're better off never trying to treat it as a number.
The same goes for "social security numbers".
Upvotes: 2
Reputation: 5187
5554567899 as a number is too big to fit in an int. Try Long.parseLong
instead.
Upvotes: 1
Reputation: 114817
The value exceeds the int range. Parse it into a long variable.
Simple test:
int i = 5554567899;
is a compile time error: "The literal 5554567899 of type int is out of range"
Upvotes: 7