当一个字符紧接着另一个字符重复时,数组索引超出范围(Java)
错误行是 13。错误是数组索引越界。 http://pastebin.com/L3FtzarH
方法是在字符串中查找字符,效果很好除非同一个字符紧邻有重复字符,例如在单词“Rabbit”中,给我一个字母“b”的错误,因为它是两次并且紧邻同一个字母。
我正在玩刽子手游戏。 我将索引乘以 to 作为空格。例如:如果秘密单词是“cat”,那么它将出现3个下划线:“_ _ _”该下划线字符串有6个字符,因此如果字母是A(世界“cat”中的索引1)我乘以2填充字符串中的正确位置 下划线:“_ A _”
编辑 1,您的代码:
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;
}
}
}
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;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在
if (chequearif == true) {
块内,您尝试在indexOf()
调用中使用index
。您试图在第一次出现后检查更多出现的情况,您认为index
指向该出现,但您忘记了之前已将索引乘以 2。因此它指向第二个之外的某个位置发生,因此indexOf()
返回 -1。您将其加倍为 -2,然后尝试说 arrayGuiones[index],这显然永远不会与负索引一起使用。Inside your
if (chequearif == true) {
block, you try to useindex
in anindexOf()
call. You're trying to check for more occurrences after the first occurrence, which you thinkindex
points 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.