1.

Solve : Java program issues?

Answer»

I made this program for a class. Its purpose is to convert sequences of numbers from the keyboard to text strings based on the usual multi-tap coding used with cell phones. For EXAMPLE, 4433555 555666 = "hello".

My strategy was to create a method (convertToLetter) which takes the number entered and number of times the number is entered and to SPIT it out into a String variable then print it out. I organized the input by splitting the input into arrays, splitting it at the space.

My code compiles fine so far but when I enter input I get this message.

java.lang.ArrayIndexOutOfBoundsExceptio n: 1
at Pay.main(Pay.java:124)
at sun.reflect.NativeMethodAccessorImpl.in voke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.in voke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImp l.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at edu.rice.cs.drjava.model.compiler.Javac Compiler.runCommand(JavacCompiler.java:271)

Here's my code:


import java.util.Scanner;

class Pay {

static char convertToLetter(int num, int numTimes) {

if (num == 2) {
numTimes = numTimes % 3;
switch (numTimes) {
case 1:
return 'a';
case 2:
return 'b';
case 3:
return 'c';
default:
System.out.println("num2");
}
} else if (num == 3) {
numTimes = numTimes % 3;
switch (numTimes) {
case 1:
return 'd';
case 2:
return 'e';
case 3:
return 'f';
default:
System.out.println("num3");
}
} else if (num == 4) {
numTimes = numTimes % 3;
switch (numTimes) {
case 1:
return 'g';
case 2:
return 'h';
case 3:
return 'i';
default:
System.out.println("num4");
}
} else if (num == 5) {
numTimes = numTimes % 3;
switch (numTimes) {
case 1:
return 'j';
case 2:
return 'k';
case 3:
return 'l';
default:
System.out.println("num5");
}
} else if (num == 6) {
numTimes = numTimes % 3;
switch (numTimes) {
case 1:
return 'm';
case 2:
return 'n';
case 3:
return 'o';
default:
System.out.println("num6");
}
} else if (num == 7) {
numTimes = numTimes % 4;
switch (numTimes) {
case 1:
return 'p';
case 2:
return 'q';
case 3:
return 'r';
case 4:
return 's';
default:
System.out.println("num7");
}
} else if (num == {
numTimes = numTimes % 3;
switch (numTimes) {
case 1:
return 't';
case 2:
return 'u';
case 3:
return 'v';
default:
System.out.println("num8");
}
} else if (num == 9) {
numTimes = numTimes % 4;
switch (numTimes) {
case 1:
return 'w';
case 2:
return 'X';
case 3:
return 'y';
case 4:
return 'Z';
default:
System.out.println("num9");
}
}
return 'a';
}

public static void main(String[] args) throws NumberFormatException {

Scanner sc = new Scanner(System.in);
String in = sc.nextLine();
String[] splitString = in.split(" "); // splits input into array of
// strings
String toOutput = "";
char currChar;
int numTimes = 1;


for (int index = 0; index <= splitString.length; index++) {
currChar = splitString[index].charAt(0);

for (int i = 1; i < splitString[index].length(); i++) {
if (splitString[index].charAt(i) != currChar) {
String numString = Character.toString(currChar);
int num = Integer.parseInt(numString);
toOutput += convertToLetter(num, numTimes);
currChar = splitString[index].charAt(i);
numTimes = 1;
} else {
numTimes++;
}
}
}

System.out.println(toOutput);
}

}




If anyone could help me with this it would be MUCH appreciated



Discussion

No Comment Found