简体   繁体   中英

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. 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?

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 .

         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.

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. 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.)

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 . There are other Java style guides too ... take your pick. 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...

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. Pehraps you used words in the place of word?

A debugger makes it clear: Your index y has a value of 3 when it's executed on word "ma". 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;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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