NewbieNick
NewbieNick

Reputation: 89

NumberFormatException for input String, confused as to why?

I made a method within a class that is supposed to parse a file with the format: name, xxx-xxx-xxxx for every line. I'm trying to grab the phone number and put every digit into an array of ints and return that array. Here is my code.

This is the line that causes the error---

theIntNumber=Integer.parseInt(justAnotherString);

If I had: Nicholas James, 912-345-6789

then....

justAnotherString = "9123456789" and it throws an error when trying to parse that string for an int. I'm confused as to why this is happening, shouldn't it parse the int from this string?

Thank you for any answers.

public int[] getPhoneNumberArray() throws FileNotFoundException
{
    Scanner scan = new Scanner(file);
    while(scan.hasNextLine())
    {
        String thePhoneNumber = "";
        String justAnotherString = "";
        int theIntNumber=0;
        String line = scan.nextLine();
        Scanner parser = new Scanner(line);
        parser.useDelimiter(",");
        parser.next();
        parser.useDelimiter(" ");
        parser.next();
        thePhoneNumber = parser.next();
        Scanner lol = new Scanner(thePhoneNumber);
        lol.useDelimiter("-");
        justAnotherString += lol.next();
        justAnotherString += lol.next();
        justAnotherString += lol.next();
        theIntNumber=Integer.parseInt(justAnotherString);

        for(int i=10;i>0;i--)
        {
            phoneNumberArray[i-1]=theIntNumber%10;
        }
    }
    for(int a=0;a<10;a++)
    {
        System.out.println("Phone Number: ");
    }
    return phoneNumberArray;
}

EDIT: Previous number was 123-456-7890. The number I had before was larger than the 2.1 billion that java can handle. 987-654-3210 is a better example.

This is the error I'm getting.

Exception in thread "main" java.lang.NumberFormatException: For input string: "9876543210"
    at java.lang.NumberFormatException.forInputString(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at exam2.Contact.getPhoneNumberArray(Contact.java:71)
    at exam2.ExamTwoInput.main(ExamTwoInput.java:83)

Upvotes: 0

Views: 3806

Answers (3)

IRvanFauziE
IRvanFauziE

Reputation: 852

I know this question is so old, but till now not have accepted answer.

as your note:

EDIT: Previous number was 123-456-7890. The number I had before was larger than the 2.1 billion that java can handle. 987-654-3210 is a better example.

so, please use Long.valueOf(str) instead of Integer.parseInt(str) or Integer.valueOf(str)

hope this help u all :)

Upvotes: 0

Breand&#225;n Dalton
Breand&#225;n Dalton

Reputation: 1639

As an aside, you can't store a telephone number in an integer. It loses both leading zeroes, and the plus sign.

For instance, imagine you were given the number for the British Prime Minister, +44 20 7925 0918. The plus sign there indicates that the call is an international call. It would be replaced by 011 in the North American Numbering Plan, or by 00 in the European Telephony Numbering Space. How would you represent that telephone number as an int?

Upvotes: 1

Elliott Frisch
Elliott Frisch

Reputation: 201429

Your code is working here, the only issue I see is that your formula for converting the digits of the int number to an array appears flawed. You don't need the int anyway, since you have it as justAnotherString. Basically, you could use Character.digit(char, int) like -

int[] phoneNumberArray = new int[justAnotherString.length()];
for (int i = 0; i < justAnotherString.length(); i++) {
    phoneNumberArray[i] = Character.digit(justAnotherString.charAt(i), 10);
}
System.out.println(Arrays.toString(phoneNumberArray));

Of course, you could also parse the telephone number with something like

String line = "Nicholas James, 123-456-7890";
// get everything after the last space.
String phone = line.substring(line.lastIndexOf(' ') + 1);
phone = phone.replace("-", ""); // remove the '-' symbols
int[] phoneNumberArray = new int[phone.length()];
for (int i = 0; i < phone.length(); i++) {
    phoneNumberArray[i] = Character.digit(phone.charAt(i), 10);
}
System.out.println(Arrays.toString(phoneNumberArray));

which has the advantage that it will work with (and without) dashes.

Upvotes: 1

Related Questions