The error line is 13. The error is Array index out of bound.
http://pastebin.com/L3FtzarH
The method is to look for a character into a string, and it works fine except when there are repeat characters immediately next to the same character, for example in the word “Rabbit” give me a error with the letter “b” cause it’s twice and next to the same letter.
I’m doing the hangman game.
I multiply index by to for the blank spaces. For example: if the secret word is “cat” then it will appear 3 underscores: “_ _ _ ” that underscore string has 6 characters, so if the letter is A (index 1 in the world “cat”) I multiply by 2 to fill the right place in the string
underscores: “_ A _ “
Edit 1, Your Code:
public void buscarLetra() {
chequearif = false;
string = "";
letra = letraEscogida.toCharArray();
System.out.println("la letra pasa a char:" + letra[0]);
int index = 0;
for (int i = 0; i < (palabraElegida.length()); i++) {
if (palabraElegida.substring(i, i + 1).equals(letraEscogida) == true) {
if (chequearif == true) {
string = "";
index = palabraElegida.indexOf(letraEscogida, index + 1);
index *= 2;
arrayGuiones[index] = letra[0];
System.out.println("segundo index: " + index);
index = 0;
}
index = palabraElegida.indexOf(letraEscogida);
index *= 2;
System.out.println("primer index: " + index);
arrayGuiones[index] = letra[0];
for (int j = 0; j < arrayGuiones.length; j++) {
string += arrayGuiones[j] + "";
}
lbl_palabra.setText(string);
chequearif = true;
}
}
}
Inside your
if (chequearif == true) {block, you try to useindexin anindexOf()call. You’re trying to check for more occurrences after the first occurrence, which you thinkindexpoints to, but you’ve forgotten that you’ve previously multiplied index by 2. It’s therefore pointing somewhere beyond the second occurrence, soindexOf()returns -1. You double that to -2, and then try to sayarrayGuiones[index], which will obviously never work with a negative index.