当我尝试运行我的程序,它提供了以下错误的根源......字符串索引出界失误,找不到错误
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;
}
}
}
}
}
请妥善格式化你的代码。缩进到处都是。 (请不要责怪你或你的IDE,我们以前都听说过) –
我真的不知道你想告诉我什么,对不起。 我的代码看起来有点凌乱是的,但那是因为没有人教我如何我应该这样做呢。至于SO或IDE。我没有IDEa意味着什么。 – Rdhao
解决这个混乱的最好方法是在调试器中逐步完成。这比世界上要求的要快。 – duffymo