简体   繁体   English

字符串索引超出范围错误,无法找到错误的根源

[英]String index out of bounds error, cant find the root of the error

When I try to run my program, it gives the following error... 当我尝试运行程序时,出现以下错误...

 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)

I understand that the String probably went over the boundaries of an array, but I can't seem to understand why. 我知道String可能超出了数组的边界,但是我似乎不明白为什么。 Could someone please assist me to understand the problem. 有人可以帮我理解这个问题。

This is my code... 这是我的代码...

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;
                }
            }
        }
    }
}

您无需在内部循环中重置y ,当循环继续到第二个单词时, y为3,但是words[x].charAt(y) (其中x = 1)不存在-超出范围。

Maybe you should clear y after last for? 也许您应该在最后之后清除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;

check it 核实

数组单词中的第二个单词仅由2个字母组成,并且您尝试通过以下代码word [x] .charAt(y)访问该单词中的第四个字母,但y等于3 ,对于这个词

For some words the index specified by y does not exist . 对于某些单词 ,由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 .

Try resetting y properly in the loop to give expected behavior. 尝试在循环中正确重置y以获得预期的行为。

You can analyze your program by debugging it to find out such mistakes. 您可以通过调试程序来分析程序,以找出此类错误。

I really have not a clue what youre trying to tell me, sorry mate. 对不起,我真的不知道您想告诉我什么。 My code look a bit messy yes but thats cause no ones has taught me how i should do it yet. 我的代码看起来有些混乱,是的,但是那没有人教过我该怎么做。 as for a SO or IDE. 至于SO或IDE。 i SO have no IDEa what that means. 我没有任何想法。

(IDE stands for Interactive Development Environment. For instance Eclipse, NetBeans, IDEA, etc. SO stands for Stack Overflow. I'm referring to the simple wiki language that SO provides for writing and formatting Questions and Answers.) (IDE代表交互式开发环境。例如Eclipse,NetBeans,IDEA等。SO代表堆栈溢出。我指的是SO提供的用于编写和格式化问题和答案的简单Wiki语言。)

If you haven't learned / been taught the rules of Java style, you should spend the time to read this document - Code Conventions for the Java TM Programming Language . 如果您还没有学习过Java风格的规则,则应该花些时间阅读本文档-Java TM编程语言的代码约定 There are other Java style guides too ... take your pick. 还有其他Java样式指南...请选择。 One of the key points of good style is to indent the code consistently. 好的样式的关键点之一是使代码一致地缩进。

The reason for style conventions is to make it easier to read your own and other peoples' code. 之所以采用样式约定,是为了使其更易于阅读您自己和他人的代码。 And to avoid the incidence of workmates lobbing stink bombs into your cubicle. 并避免同事将臭味炸弹刺入您的小隔间的情况。

(If you think I'm being tough on you, just wait until you experience your first full-on code review in a workplace environment ... ) (如果您认为我对您很苛刻,请等到您在工作场所环境中经历第一次全面的代码审查时……)

The problem with one loop into another is that most of the time we forgot to reinicialize the variables that we use to do the loop. 一个循环到另一个循环的问题在于,大多数时候我们忘记重新初始化用于循环的变量。 I recommend you to after every loop, reinicialize the variables that you may changed, sometimes is a innecesary step but its a good routine... 我建议您在每个循环之后重新初始化可能更改的变量,有时这是一个不必要的步骤,但它是一个很好的例程...

As Nim says the problem is that y is never re-initialized... 就像Nim所说的那样,问题是y从未被初始化过...

I can't figure what the code is for but I see the check method takes in input a word param but it doesn't use it. 我无法弄清楚代码的用途,但是我看到check方法接受了输入的单词param,但是它没有使用它。 Pehraps you used words in the place of word? Pehraps您用单词代替单词了吗?

A debugger makes it clear: Your index y has a value of 3 when it's executed on word "ma". 调试器清楚地表明:对单词“ ma”执行索引y的值为3。 You shouldn't be using that loop value in that scope. 您不应该在该范围内使用该循环值。

I'm not sure I understand your logic, but it shouldn't take too long. 我不确定我是否理解您的逻辑,但不要花太长时间。

Your reformatted code would look like this: 重新格式化的代码如下所示:

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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM