-1

I am trying to write a class that takes a phone number that the user inputs as a string, then stores each digit in an array. I converted the string to a long using Long.parseLong. Here is the function that is supposed to store each individual digit in an array.

public void storetoarray()
        //Turn the strings to longs
        //A loop chops of the last digit and stores in an array
{
    phonelong = Long.parseLong(initialphone);
    phonetemp1 = phonelong;
    for (int i = phonelength; i>=0; i--)
    {
        phonearray[i-1] = phonetemp1%10;
        phonetemp2 = phonetemp1 - phonetemp1%10;
        phonetemp1 = phonetemp2/10;
        System.out.print("Phone temp 2" + phonetemp2 + phonetemp1);
    }
}

The logic of that loop is to find the last digit of the phone number using a %10, then subtract that number from the original phone number. That new difference is then set to be phonetemp1/10.

Example 4158884532%10 = 2. That is stored in phonearray[9]. Then phonetemp2 is subtracted giving us 4158884530. Divide that by 10 and you get 415888453. Should be ready to store the next digit.

What is going wrong? Thanks for the help!

Dang Nguyen
  • 1,209
  • 1
  • 17
  • 29
Max
  • 837
  • 4
  • 11
  • 20
  • 1
    Why bother converting to long and then trying to extract digits? Just use substring(). – mclaassen Jun 01 '13 at 00:40
  • When do you create phoneArray? Also, why so many phone variables? Mclaassen is right. In any case, you're being North American Numbering Plan-centric. And, where does the input come from in here? – Eric Jablow Jun 01 '13 at 00:44
  • Phone numbers should be treated as strings and not numbers. – Don Roby Jun 01 '13 at 00:50
  • Show these declarations - `phonelength`, `initialphone`, `phonearray`. You are only giving us partial information. – xagyg Jun 01 '13 at 02:05
  • You should provide the full code, including *the input that causes the error*, and (while you are at it) the error itself. Asking "what is going wrong" without showing what wrong is makes it unnecessarily hard to answer. – tucuxi Apr 21 '21 at 10:14

2 Answers2

0

Your phonearray does not have enough space: you are accessing index 9, but phonearray has fewer than ten elements.

You need to allocate the array like this:

phonearray = new int[10];

Once you fix this problem, change the loop to avoid accessing index -1 (that's what is going to happen when i reaches zero).

Finally, there is no need to subtract phonetemp1%10 from phonetemp1: integer division drops the fraction, so you could do this:

phonearray[i-1] = phonetemp1%10;
phonetemp1 /= 10;
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • I declared phone array like this long[] phonearray = new long[phonelength]; where phonelength = initialphone.length(); Doesnt that mean the array will have 10 elements then if initialphone.length() is 10? – Max Jun 01 '13 at 00:50
  • @user2437416 Then the `ArrayIndexOutOfBoundsException: 9` comes from a different place: if the length of the array is 10, then 9 is a valid index. Your code still has the potential of accessing element -1, when i==0, but then the exception would read `ArrayIndexOutOfBoundsException: -1`. – Sergey Kalinichenko Jun 01 '13 at 00:54
0

If you have initialized the array correctly with the size same as the length of the initialPhone, you should not see this exception. Your code would rather produce ArrayIndexOutOfBoundException: -1 Rewrote your code a bit, and works as expected:

import java.util.Arrays;

public class TestMain {

  public static void main(String[] args) {

    String initialPhone = "4158884532";
    int phoneLength = initialPhone.length();
    long[] phoneArray = new long[phoneLength];

    Long phoneNum = Long.parseLong(initialPhone);
    Long phonetemp1 = phoneNum;
    for (int i = phoneLength; i > 0; i--) {
      phoneArray[i - 1] = phonetemp1 % 10;
      Long phonetemp2 = phonetemp1 - phonetemp1 % 10;
      phonetemp1 = phonetemp2 / 10;
      System.out.println("Phone temp 2 --> " + phonetemp2 + phonetemp1);
    }
    System.out.println(Arrays.toString(phoneArray));
  }
}

Enjoy!

moonlighter
  • 541
  • 3
  • 14