0

当我尝试运行我的程序时,它给出了以下错误......

 Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 3
    at java.lang.String.charAt(Unknown Source)
    at Woordzoeker.check(Woordzoeker.java:88)
    at Woordzoeker.main(Woordzoeker.java:8)

我知道这String可能超出了数组的边界,但我似乎不明白为什么。有人可以帮助我理解这个问题。

这是我的代码...

public class Woordzoeker {
    public static String[] words = {"boom","ma","maat","kas","kast","as","boek","boot"};
    public static String[][] grid = {{"b","o","e","k"},{"o","o","z","a"},{"o","j","o","s"},{"m","a","a","t"}};
    public static String[][] gridz = new String[4][4];

    public static void main(String[] args) {
        for (int x=0; x < words.length-1; x++){
            System.out.println(words[x] + " --> " + check(words[x],grid));
        } // THIS IS LINE 8
        System.out.println(isCorrectGrid(grid));
        System.out.println(isCorrectWords(words));
        }

public static int[] check(String word, String[][] grid){
    int[] array = new int[3];
    int y = 0;
    for (int rij=0; rij < grid.length; rij++){
        for (int kolom = 0;kolom < grid[rij].length; kolom++){
            for (int x=0; x < words.length - 1; x++)
                if (words[x].charAt(y) == (grid[rij][kolom].charAt(0))){  // THIS IS LINE 88
                    array[0] = rij;
                    array[1] = kolom;  // slaat de begin coordinaten op
            for (y = 0; y < words[x].length() - 1; y++){
                if (words[x].charAt(y) == grid[rij + y][kolom].charAt(0)){
                    array[2] = 1;
                }
                if (words[x].charAt(y) == (grid[rij][kolom + y].charAt(0))){
                    array[2] = 2;
                }
                if (words[x].charAt(y) == (grid[rij + y][kolom + y].charAt(0))){
                    array[2] = 3;
                }
            }
        }
    }
}
4

8 回答 8

1

您不会y在内部循环中重置,当循环进行到第二个单词时,y是 3,但是words[x].charAt(y)(where x= 1) 不存在 - 它超出了范围。

于 2011-12-12T10:18:36.260 回答
1

也许你应该在最后一次之后清除 y?

for (y = 0; y < words[x].length() - 1; y++){
   if (words[x].charAt(y) == grid[rij + y][kolom].charAt(0)){
   array[2] = 1;
   }
   if (words[x].charAt(y) == (grid[rij][kolom + y].charAt(0))){
   array[2] = 2;
   }
   if (words[x].charAt(y) == (grid[rij + y][kolom + y].charAt(0))){
   array[2] = 3;
   }

}
y=0;

核实

于 2011-12-12T10:22:12.563 回答
1

数组words中的第 2 个单词仅由 2 个字母组成,您正尝试通过以下代码words[x].charAt(y)访问该单词中的第 4 个字母,但 y 等于3,超出范围这个词

于 2011-12-12T10:24:00.833 回答
0

我不知道代码是做什么用的,但我看到 check 方法接受输入一个单词参数,但它不使用它。Pehraps 你用词代替词吗?

于 2011-12-12T10:28:44.427 回答
0

一个循环进入另一个循环的问题在于,大多数时候我们忘记重新初始化用于执行循环的变量。我建议您在每个循环之后,重新初始化您可能更改的变量,有时这是一个不必要的步骤,但它是一个很好的例程......

正如 Nim 所说,问题是y永远不会重新初始化......

于 2011-12-12T10:26:06.917 回答
0

对于某些单词,由 y 指定的索引不存在。

         if (words[x].charAt(y) == (grid[rij][kolom].charAt(0))) //in this line If the words is "ma" then charAt(y=3) does not exist ,hence the error .

尝试在循环中正确重置 y 以提供预期的行为。

您可以通过调试程序来分析您的程序以找出此类错误。

于 2011-12-12T10:21:14.853 回答
0

调试器清楚地表明:y在单词“ma”上执行时,您的索引值为 3。您不应该在该范围内使用该循环值。

我不确定我是否理解你的逻辑,但应该不会花太长时间。

您重新格式化的代码如下所示:

public class Woordzoeker {
    public static String[] words = {"boom", "ma", "maat", "kas", "kast", "as", "boek", "boot"};
    public static String[][] grid = {{"b", "o", "e", "k"}, {"o", "o", "z", "a"}, {"o", "j", "o", "s"}, {"m", "a", "a", "t"}};
    public static String[][] gridz = new String[4][4];

    public static void main(String[] args) {
        for (int x = 0; x < words.length - 1; x++) {
/* --> */
            System.out.println(words[x] + " --> " + check(words[x], grid));
        }
    }

    public static int[] check(String word, String[][] grid) {


        int[] array = new int[3];
        int y = 0;
        for (int rij = 0; rij < grid.length; rij++) {
            for (int kolom = 0; kolom < grid[rij].length; kolom++) {
                for (int x = 0; x < words.length - 1; x++) {
                    /*-->*/
                    if (words[x].charAt(y) == (grid[rij][kolom].charAt(0))) {
                        array[0] = rij;
                        array[1] = kolom;  // slaat de begin coordinaten op
                        for (y = 0; y < words[x].length() - 1; y++) {
                            if (words[x].charAt(y) == grid[rij + y][kolom].charAt(0)) {
                                array[2] = 1;
                            }
                            if (words[x].charAt(y) == (grid[rij][kolom + y].charAt(0))) {
                                array[2] = 2;
                            }
                            if (words[x].charAt(y) == (grid[rij + y][kolom + y].charAt(0))) {
                                array[2] = 3;
                            }

                        }
                    }
                }
            }
        }

        return array;
    }
}
于 2011-12-12T10:36:38.357 回答
0

我真的不知道你想告诉我什么,对不起,伙计。我的代码看起来有点乱,是的,但那是因为没有人教我应该怎么做。至于 SO 或 IDE。我不知道那是什么意思。

(IDE 代表交互式开发环境。例如 Eclipse、NetBeans、IDEA 等。SO 代表 Stack Overflow。我指的是 SO 提供的用于编写和格式化问题和答案的简单 wiki 语言。)

如果您还没有学习/被教过 Java 风格的规则,您应该花时间阅读本文档 - Java TM 编程语言的代码约定。还有其他 Java 风格指南……任你挑选。良好风格的关键点之一是一致地缩进代码。

样式约定的原因是为了更容易阅读您自己和其他人的代码。并避免同事向您的隔间投掷臭气弹。

(如果你认为我对你很严厉,那就等到你在工作环境中体验到第一次全面的代码审查......)

于 2011-12-12T10:24:20.677 回答