当一个字符紧接着另一个字符重复时,数组索引超出范围(Java)

发布于 2024-12-09 02:52:18 字数 1458 浏览 2 评论 0原文

错误行是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

温柔一刀 2024-12-16 02:52:18

if (chequearif == true) { 块内,您尝试在 indexOf() 调用中使用 index 。您试图在第一次出现后检查更多出现的情况,您认为 index 指向该出现,但您忘记了之前已将索引乘以 2。因此它指向第二个之外的某个位置发生,因此 indexOf() 返回 -1。您将其加倍为 -2,然后尝试说 arrayGuiones[index],这显然永远不会与负索引一起使用。

Inside your if (chequearif == true) { block, you try to use index in an indexOf() call. You're trying to check for more occurrences after the first occurrence, which you think index points to, but you've forgotten that you've previously multiplied index by 2. It's therefore pointing somewhere beyond the second occurrence, so indexOf() returns -1. You double that to -2, and then try to say arrayGuiones[index], which will obviously never work with a negative index.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文