So, I'm trying to make a method by which you send a simple string, which is then split into char on char in a for loop, each char from the array "alpha" is converted to its equivalent value in "beta", they are then combined using StringBuilder and sent back as an "exit" as shown below:
Code:
I simplified the entire class and all variable names to improve readability.
import java.util.Arrays;
public class Test {
private final char[] alpha = { 'A', 'B', 'C', 'D', 'E',
'F', 'G', 'H', 'I', 'J',
'K', 'L', 'M', 'N', 'O',
'P', 'Q', 'R', 'S', 'T',
'U', 'V', 'W', 'X', 'Y', 'Z'};
public String getAlpha(String input) {
StringBuilder output = new StringBuilder();
for (int i = 0; i < input.length(); i++) {
char c = input.charAt(i);
int index;
if (Character.isLetter(c)) {
try {
index = Arrays.asList(alpha).indexOf(Character.toUpperCase(c));
System.out.println(c);
System.out.println(index);
} catch(ArrayIndexOutOfBoundsException e) {
e.printStackTrace();
}
} else {
output.append(c);
}
}
return output.toString();
}
public static void main(String[] args) {
Test test1 = new Test();
test1.getAlpha("Hello");
}
}
The problem is that my index for each char sequentially throws an ArrayIndexOutOfBounds exception due to the fact that indexOf cannot find the equivalent of characters in the alpha array.
- ? , , / char. .
:
"Hello"
:
H
-1
e
-1
l
-1
l
-1
o
-1