Hey everyone. I have been playing around with this code and have been getting this error message:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 7
at java.lang.String.charAt(String.java:686)
at PhoneNumber.main(PhoneNumber.java:37)
Java Result: 1
BUILD SUCCESSFUL (total time: 8 seconds)

I understand what the java.lang.StringIndexOutOfBoundsException means but I don;t understand what is causing this. If anyone has any thoughts on this I would appreciate your input.

import java.util.Scanner;



public class PhoneNumber {


public static void main(String[] Args){


char number2[] = { 'A', 'B', 'C' };
char number3[] = { 'D', 'E', 'F' };
char number4[] = { 'G', 'H', 'I' };
char number5[] = { 'J', 'K', 'L' };
char number6[] = { 'M', 'N', 'O' };
char number7[] = { 'P', 'R', 'S' };
char number8[] = { 'T', 'U', 'V' };
char number9[] = { 'W', 'X', 'Y' };

char Letter0[] = { ' ',' ',' ' };
char Letter1[] = { ' ',' ',' ' };
char Letter2[] = { ' ',' ',' ' };
char Letter3[] = { ' ',' ',' ' };
char Letter4[] = { ' ',' ',' ' };
char Letter5[] = { ' ',' ',' ' };
char Letter6[] = { ' ',' ',' ' };
char Letter7[] = { ' ',' ',' ' };

Scanner scanner = new Scanner( System.in );
Character phoneArray[] = new Character[ 7 ];

System.out.print( "Enter a phone: " );
String phoneNumber = scanner.nextLine();

//Convert phoneNumber string to array of characters
for( int i = 0; i <= phoneNumber.length(); i++ ) {
phoneArray[i]=(new Character(phoneNumber.charAt( i )));
}

for (int i=0;i < phoneArray.length; i++) {
if ( phoneArray[i].equals('0') ){
System.out.println("Invalid Input (0)");
System.exit(1);
}
else if ( phoneArray[i].equals('1') ){
System.out.println("Invalid Input (1)");
System.exit(1);
}

        }


//COMPARE FIRST DIGIT
if (phoneArray[0].equals('2'))
        System.arraycopy(number2, 0, Letter0, 0, Letter0.length);
else if (phoneArray[0].equals('3'))
        System.arraycopy(number3, 0, Letter0, 0, Letter0.length);
else if (phoneArray[0].equals('4'))
        System.arraycopy(number4, 0, Letter0, 0, Letter0.length);
else if (phoneArray[0].equals('5'))
        System.arraycopy(number5, 0, Letter0, 0, Letter0.length);
else if (phoneArray[0].equals('6'))
        System.arraycopy(number6, 0, Letter0, 0, Letter0.length);
else if (phoneArray[0].equals('7'))
        System.arraycopy(number7, 0, Letter0, 0, Letter0.length);
else if (phoneArray[0].equals('8'))
        System.arraycopy(number8, 0, Letter0, 0, Letter0.length);
else if (phoneArray[0].equals('9'))
        System.arraycopy(number9, 0, Letter0, 0, Letter0.length);

//COMPARE SECOND DIGIT
if (phoneArray[1].equals('2'))
        System.arraycopy(number2, 0, Letter1, 0, Letter1.length);
else if (phoneArray[1].equals('3'))
        System.arraycopy(number3, 0, Letter1, 0, Letter1.length);
else if (phoneArray[1].equals('4'))
        System.arraycopy(number4, 0, Letter1, 0, Letter1.length);
else if (phoneArray[1].equals('5'))
        System.arraycopy(number5, 0, Letter1, 0, Letter1.length);
else if (phoneArray[1].equals('6'))
        System.arraycopy(number6, 0, Letter1, 0, Letter1.length);
else if (phoneArray[1].equals('7'))
        System.arraycopy(number7, 0, Letter1, 0, Letter1.length);
else if (phoneArray[1].equals('8'))
        System.arraycopy(number8, 0, Letter1, 0, Letter1.length);
else if (phoneArray[1].equals('9'))
        System.arraycopy(number9, 0, Letter1, 0, Letter1.length);

//COMPARE THIRD DIGIT
if (phoneArray[2].equals('2'))
        System.arraycopy(number2, 0, Letter2, 0, Letter2.length);
else if (phoneArray[2].equals('3'))
        System.arraycopy(number3, 0, Letter2, 0, Letter2.length);
else if (phoneArray[2].equals('4'))
        System.arraycopy(number4, 0, Letter2, 0, Letter2.length);
else if (phoneArray[2].equals('5'))
        System.arraycopy(number5, 0, Letter2, 0, Letter2.length);
else if (phoneArray[2].equals('6'))
        System.arraycopy(number6, 0, Letter2, 0, Letter2.length);
else if (phoneArray[2].equals('7'))
        System.arraycopy(number7, 0, Letter2, 0, Letter2.length);
else if (phoneArray[2].equals('8'))
        System.arraycopy(number8, 0, Letter2, 0, Letter2.length);
else if (phoneArray[2].equals('9'))
        System.arraycopy(number9, 0, Letter2, 0, Letter2.length);

//COMPARE FORTH DIGIT
if (phoneArray[3].equals('2'))
        System.arraycopy(number2, 0, Letter3, 0, Letter3.length);
else if (phoneArray[3].equals('3'))
        System.arraycopy(number3, 0, Letter3, 0, Letter3.length);
else if (phoneArray[3].equals('4'))
        System.arraycopy(number4, 0, Letter3, 0, Letter3.length);
else if (phoneArray[3].equals('5'))
        System.arraycopy(number5, 0, Letter3, 0, Letter3.length);
else if (phoneArray[3].equals('6'))
        System.arraycopy(number6, 0, Letter3, 0, Letter3.length);
else if (phoneArray[3].equals('7'))
        System.arraycopy(number7, 0, Letter3, 0, Letter3.length);
else if (phoneArray[3].equals('8'))
        System.arraycopy(number8, 0, Letter3, 0, Letter3.length);
else if (phoneArray[3].equals('9'))
        System.arraycopy(number9, 0, Letter3, 0, Letter3.length);

//COMPARE FIFTH DIGIT
if (phoneArray[4].equals('2'))
        System.arraycopy(number2, 0, Letter4, 0, Letter4.length);
else if (phoneArray[4].equals('3'))
        System.arraycopy(number3, 0, Letter4, 0, Letter4.length);
else if (phoneArray[4].equals('4'))
        System.arraycopy(number4, 0, Letter4, 0, Letter4.length);
else if (phoneArray[4].equals('5'))
        System.arraycopy(number5, 0, Letter4, 0, Letter4.length);
else if (phoneArray[4].equals('6'))
        System.arraycopy(number6, 0, Letter4, 0, Letter4.length);
else if (phoneArray[4].equals('7'))
        System.arraycopy(number7, 0, Letter4, 0, Letter4.length);
else if (phoneArray[4].equals('8'))
        System.arraycopy(number8, 0, Letter4, 0, Letter4.length);
else if (phoneArray[4].equals('9'))
        System.arraycopy(number9, 0, Letter4, 0, Letter4.length);

//COMPARE SIXTH DIGIT
if (phoneArray[5].equals('2'))
        System.arraycopy(number2, 0, Letter5, 0, Letter5.length);
else if (phoneArray[5].equals('3'))
        System.arraycopy(number3, 0, Letter5, 0, Letter5.length);
else if (phoneArray[5].equals('4'))
        System.arraycopy(number4, 0, Letter5, 0, Letter5.length);
else if (phoneArray[5].equals('5'))
        System.arraycopy(number5, 0, Letter5, 0, Letter5.length);
else if (phoneArray[5].equals('6'))
        System.arraycopy(number6, 0, Letter5, 0, Letter5.length);
else if (phoneArray[5].equals('7'))
        System.arraycopy(number7, 0, Letter5, 0, Letter5.length);
else if (phoneArray[5].equals('8'))
        System.arraycopy(number8, 0, Letter5, 0, Letter5.length);
else if (phoneArray[5].equals('9'))
        System.arraycopy(number9, 0, Letter5, 0, Letter5.length);

//COMPARE SEVENTH DIGIT
if (phoneArray[6].equals('2'))
        System.arraycopy(number2, 0, Letter6, 0, Letter6.length);
else if (phoneArray[6].equals('3'))
        System.arraycopy(number3, 0, Letter6, 0, Letter6.length);
else if (phoneArray[6].equals('4'))
        System.arraycopy(number4, 0, Letter6, 0, Letter6.length);
else if (phoneArray[6].equals('5'))
        System.arraycopy(number5, 0, Letter6, 0, Letter6.length);
else if (phoneArray[6].equals('6'))
        System.arraycopy(number6, 0, Letter6, 0, Letter6.length);
else if (phoneArray[6].equals('7'))
        System.arraycopy(number7, 0, Letter6, 0, Letter6.length);
else if (phoneArray[6].equals('8'))
        System.arraycopy(number8, 0, Letter6, 0, Letter6.length);
else if (phoneArray[6].equals('9'))
        System.arraycopy(number9, 0, Letter6, 0, Letter6.length);

//PRINT
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
for (int k = 0; k < 3; k++) {
for (int l = 0; l < 3; l++) {
for (int m = 0; m < 3; m++) {
for (int n = 0; n < 3; n++) {
for (int o = 0; o<3; o++) {
System.out.print(Letter0[i]);
System.out.print(" ");
System.out.print(Letter1[j]);
System.out.print(" ");
System.out.print(Letter2[k]);
System.out.print(" ");
System.out.print(Letter3[l]);
System.out.print(" ");
System.out.print(Letter4[m]);
System.out.print(" ");
System.out.print(Letter5[n]);
System.out.print(" ");
System.out.print(Letter6[o]);
System.out.println();
}
}
}
}
} //end k for
} //end j for
} // end i for

 }    //End main
 }   // End PhoneNumber

Look at the for loop. The index's max value must be 1 less than the number of elements in the array. For example with 40 elements the max index is 39. Remember its 0 based.

Thanks guys! I changed the line 37 to this:

//Convert phoneNumber string to array of characters
for( int i = 0; i < (phoneNumber.length() - 1) ; i++ ) {
phoneArray[i]=(new Character(phoneNumber.charAt( i )));
}

and now it works, but now I am having issue with line 41. The error code says:
Exception in thread "main" java.lang.NullPointerException
at PhoneNumber.main(PhoneNumber.java:41)
Java Result: 1

Am I getting a null exception because of the "0" in the

( phoneArray[i].equals('0')

?

Thanks for you guys help!

Just figured it out. The line before it was for

(int i = 0 ; i < (phoneArray.length ) ; i++)

and needed to be

for (int i = 0 ; i < (phoneArray.length - 1) ; i++)

I am aware that arrays start counting at 0. I am just a newb. Haha

Here is what I have so far:

import java.util.Scanner;



public class PhoneNumber {


public static void main(String[] Args){


char number2[] = { 'A', 'B', 'C' };
char number3[] = { 'D', 'E', 'F' };
char number4[] = { 'G', 'H', 'I' };
char number5[] = { 'J', 'K', 'L' };
char number6[] = { 'M', 'N', 'O' };
char number7[] = { 'P', 'R', 'S' };
char number8[] = { 'T', 'U', 'V' };
char number9[] = { 'W', 'X', 'Y' };

char Letter0[] = { ' ',' ',' ' };
char Letter1[] = { ' ',' ',' ' };
char Letter2[] = { ' ',' ',' ' };
char Letter3[] = { ' ',' ',' ' };
char Letter4[] = { ' ',' ',' ' };
char Letter5[] = { ' ',' ',' ' };
char Letter6[] = { ' ',' ',' ' };


Scanner scanner = new Scanner( System.in );
Character phoneArray[] = new Character[ 7 ];

System.out.print( "Enter a phone: " );
String phoneNumber = scanner.nextLine();

//Convert phoneNumber string to array of characters
for( int i = 0; i < (phoneNumber.length() - 1  ) ; i++ ) {
phoneArray[i]=(new Character(phoneNumber.charAt( i )));
}

for (int i = 0 ; i < (phoneArray.length - 1 ) ; i++) {
if ( phoneArray[i].equals('0') ){
System.out.println("Invalid Input (0)");
System.exit(1);
}
else if ( phoneArray[i].equals('1') ){
System.out.println("Invalid Input (1)");
System.exit(1);
}

        }


//COMPARE FIRST DIGIT
if (phoneArray[0].equals('2'))
        System.arraycopy(number2, 0, Letter0, 0, Letter0.length);
else if (phoneArray[0].equals('3'))
        System.arraycopy(number3, 0, Letter0, 0, Letter0.length);
else if (phoneArray[0].equals('4'))
        System.arraycopy(number4, 0, Letter0, 0, Letter0.length);
else if (phoneArray[0].equals('5'))
        System.arraycopy(number5, 0, Letter0, 0, Letter0.length);
else if (phoneArray[0].equals('6'))
        System.arraycopy(number6, 0, Letter0, 0, Letter0.length);
else if (phoneArray[0].equals('7'))
        System.arraycopy(number7, 0, Letter0, 0, Letter0.length);
else if (phoneArray[0].equals('8'))
        System.arraycopy(number8, 0, Letter0, 0, Letter0.length);
else if (phoneArray[0].equals('9'))
        System.arraycopy(number9, 0, Letter0, 0, Letter0.length);

//COMPARE SECOND DIGIT
if (phoneArray[1].equals('2'))
        System.arraycopy(number2, 0, Letter1, 0, Letter1.length);
else if (phoneArray[1].equals('3'))
        System.arraycopy(number3, 0, Letter1, 0, Letter1.length);
else if (phoneArray[1].equals('4'))
        System.arraycopy(number4, 0, Letter1, 0, Letter1.length);
else if (phoneArray[1].equals('5'))
        System.arraycopy(number5, 0, Letter1, 0, Letter1.length);
else if (phoneArray[1].equals('6'))
        System.arraycopy(number6, 0, Letter1, 0, Letter1.length);
else if (phoneArray[1].equals('7'))
        System.arraycopy(number7, 0, Letter1, 0, Letter1.length);
else if (phoneArray[1].equals('8'))
        System.arraycopy(number8, 0, Letter1, 0, Letter1.length);
else if (phoneArray[1].equals('9'))
        System.arraycopy(number9, 0, Letter1, 0, Letter1.length);

//COMPARE THIRD DIGIT
if (phoneArray[2].equals('2'))
        System.arraycopy(number2, 0, Letter2, 0, Letter2.length);
else if (phoneArray[2].equals('3'))
        System.arraycopy(number3, 0, Letter2, 0, Letter2.length);
else if (phoneArray[2].equals('4'))
        System.arraycopy(number4, 0, Letter2, 0, Letter2.length);
else if (phoneArray[2].equals('5'))
        System.arraycopy(number5, 0, Letter2, 0, Letter2.length);
else if (phoneArray[2].equals('6'))
        System.arraycopy(number6, 0, Letter2, 0, Letter2.length);
else if (phoneArray[2].equals('7'))
        System.arraycopy(number7, 0, Letter2, 0, Letter2.length);
else if (phoneArray[2].equals('8'))
        System.arraycopy(number8, 0, Letter2, 0, Letter2.length);
else if (phoneArray[2].equals('9'))
        System.arraycopy(number9, 0, Letter2, 0, Letter2.length);

//COMPARE FORTH DIGIT
if (phoneArray[3].equals('2'))
        System.arraycopy(number2, 0, Letter3, 0, Letter3.length);
else if (phoneArray[3].equals('3'))
        System.arraycopy(number3, 0, Letter3, 0, Letter3.length);
else if (phoneArray[3].equals('4'))
        System.arraycopy(number4, 0, Letter3, 0, Letter3.length);
else if (phoneArray[3].equals('5'))
        System.arraycopy(number5, 0, Letter3, 0, Letter3.length);
else if (phoneArray[3].equals('6'))
        System.arraycopy(number6, 0, Letter3, 0, Letter3.length);
else if (phoneArray[3].equals('7'))
        System.arraycopy(number7, 0, Letter3, 0, Letter3.length);
else if (phoneArray[3].equals('8'))
        System.arraycopy(number8, 0, Letter3, 0, Letter3.length);
else if (phoneArray[3].equals('9'))
        System.arraycopy(number9, 0, Letter3, 0, Letter3.length);

//COMPARE FIFTH DIGIT
if (phoneArray[4].equals('2'))
        System.arraycopy(number2, 0, Letter4, 0, Letter4.length);
else if (phoneArray[4].equals('3'))
        System.arraycopy(number3, 0, Letter4, 0, Letter4.length);
else if (phoneArray[4].equals('4'))
        System.arraycopy(number4, 0, Letter4, 0, Letter4.length);
else if (phoneArray[4].equals('5'))
        System.arraycopy(number5, 0, Letter4, 0, Letter4.length);
else if (phoneArray[4].equals('6'))
        System.arraycopy(number6, 0, Letter4, 0, Letter4.length);
else if (phoneArray[4].equals('7'))
        System.arraycopy(number7, 0, Letter4, 0, Letter4.length);
else if (phoneArray[4].equals('8'))
        System.arraycopy(number8, 0, Letter4, 0, Letter4.length);
else if (phoneArray[4].equals('9'))
        System.arraycopy(number9, 0, Letter4, 0, Letter4.length);

//COMPARE SIXTH DIGIT
if (phoneArray[5].equals('2'))
        System.arraycopy(number2, 0, Letter5, 0, Letter5.length);
else if (phoneArray[5].equals('3'))
        System.arraycopy(number3, 0, Letter5, 0, Letter5.length);
else if (phoneArray[5].equals('4'))
        System.arraycopy(number4, 0, Letter5, 0, Letter5.length);
else if (phoneArray[5].equals('5'))
        System.arraycopy(number5, 0, Letter5, 0, Letter5.length);
else if (phoneArray[5].equals('6'))
        System.arraycopy(number6, 0, Letter5, 0, Letter5.length);
else if (phoneArray[5].equals('7'))
        System.arraycopy(number7, 0, Letter5, 0, Letter5.length);
else if (phoneArray[5].equals('8'))
        System.arraycopy(number8, 0, Letter5, 0, Letter5.length);
else if (phoneArray[5].equals('9'))
        System.arraycopy(number9, 0, Letter5, 0, Letter5.length);

//COMPARE SEVENTH DIGIT
if (phoneArray[6].equals('2'))
        System.arraycopy(number2, 0, Letter6, 0, Letter6.length);
else if (phoneArray[6].equals('3'))
        System.arraycopy(number3, 0, Letter6, 0, Letter6.length);
else if (phoneArray[6].equals('4'))
        System.arraycopy(number4, 0, Letter6, 0, Letter6.length);
else if (phoneArray[6].equals('5'))
        System.arraycopy(number5, 0, Letter6, 0, Letter6.length);
else if (phoneArray[6].equals('6'))
        System.arraycopy(number6, 0, Letter6, 0, Letter6.length);
else if (phoneArray[6].equals('7'))
        System.arraycopy(number7, 0, Letter6, 0, Letter6.length);
else if (phoneArray[6].equals('8'))
        System.arraycopy(number8, 0, Letter6, 0, Letter6.length);
else if (phoneArray[6].equals('9'))
        System.arraycopy(number9, 0, Letter6, 0, Letter6.length);

//PRINT
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
for (int k = 0; k < 3; k++) {
for (int l = 0; l < 3; l++) {
for (int m = 0; m < 3; m++) {
for (int n = 0; n < 3; n++) {
for (int o = 0; o<3; o++) {
System.out.print(Letter0[i]);
System.out.print(" ");
System.out.print(Letter1[j]);
System.out.print(" ");
System.out.print(Letter2[k]);
System.out.print(" ");
System.out.print(Letter3[l]);
System.out.print(" ");
System.out.print(Letter4[m]);
System.out.print(" ");
System.out.print(Letter5[n]);
System.out.print(" ");
System.out.print(Letter6[o]);
System.out.println();
}
}
}
}
} //end k for
} //end j for
} // end i for

 }    //End main
 }   // End PhoneNumber

If I enter a seven digit number I get the error code
Exception in thread "main" java.lang.NullPointerException
at PhoneNumber.main(PhoneNumber.java:162)
Java Result: 1
But if I enter a eight digit number I seems to work fine. I am so confused. Any thoughts??

Print out the contents of the phoneArray to see what is in it.

For example:

System.out.println("pA=" + java.util.Arrays.toString(phoneArray));

Just a brief hint (feel free to ignore this)...
Java arrays can be two-dimensional, so instead of all those number2, number3 etc variables you can have a single array
char[][] numbers = {{},{},{'A','B','C'}, {'D','E','F'},...};
then instead of all those repeated if tests you can just index the outer array, ie numbers[2] will be the array {'A','B','C'} etc.
Similarly, where you have repeated blocks of code for (phoneArray[0],(phoneArray[1] etc you can use a for loop
for (int i =0; i <=6; i++) { // code using phoneArray

In short, your 56 almost-identical if...arraycopy blocks should be just one statement using variable array indexes inside a loop.

This article has been dead for over six months. Start a new discussion instead.