I am trying to search if char from alpabet array is equal to the textCharArray’s element. And if so do some math: char number from alphabet to add to arrayList value. But I getting an error:
java.lang.ArrayIndexOutOfBoundsException
The problem should be in for cicles. But I don’t know how to fix it corectly.
The
Code:
outputText.setText("");
inputTextString = inputText.getText().toString().replace(" ", ""); //panaikinami tarpa
char[] textCharArray = inputTextString.toCharArray(); //seka paverciama char masyvu
int l = textCharArray.length;
char[] alphabet = {'A','B','C','D','E','F','G','H','I','K','L','M','N','O','P','Q','R','S','T','V','X','Y','Z'};
int alpLenght = alphabet.length;
System.out.println(alpLenght);
stringKey = inputKey.getText().toString();
int k = Integer.parseInt(stringKey);
List<Integer>keyList = new ArrayList<Integer>();
while(k > 0){
keyList.add(k%10);
k = k /10;
}
Collections.reverse(keyList);
int j = 0;
int temp;
for(int i = 0; i <= l; i++){
for(int ii = 0; ii < alpLenght; i++){
if(j < keyList.size()){
if(textCharArray[i] == alphabet[ii]){
temp = ii + keyList.get(j);
System.out.println("Uzkoduotas: " + temp);
}
j++;
}
else {
j = 0;
if(textCharArray[i] == alphabet[ii]){
temp = ii + keyList.get(j);
System.out.println("Uzkoduotas: " + temp);
}
}
j++;
}
You have an error here
You have and infinite loop because of the faulty for, and the i is being incremented until he is out of bounds.
You use ii variable but you increment i in this line:
Replace with