2011-12-12 82 views
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; 
       } 
      } 
     } 
    } 
} 
+0

请妥善格式化你的代码。缩进到处都是。 (请不要责怪你或你的IDE,我们以前都听说过) –

+0

我真的不知道你想告诉我什么,对不起。 我的代码看起来有点凌乱是的,但那是因为没有人教我如何我应该这样做呢。至于SO或IDE。我没有IDEa意味着什么。 – Rdhao

+0

解决这个混乱的最好方法是在调试器中逐步完成。这比世界上要求的要快。 – duffymo

回答

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; 

检查

+0

这没有削减它,但是在我每次重置之后,如果我停止收到错误,那么谢谢=] – Rdhao

1

你不复位y在内部循环,当循环进入第二个字,y是3,但words[x].charAt(y)(其中x = 1)不存在 - 它超出了界限。

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以给出预期的行为。

您可以通过调试它,找出这样的错误分析程序。

1

第二个字的数组中仅由2个英文字母,而您试图通过follwing代码访问该单词的第四个字母字[X]。的charAt(Y)但y等于这是出界的话

0

我真的没有什么线索你试图告诉我,对不起,伙计。我的代码看起来有点凌乱,但那是因为没有人教我如何做。至于SO或IDE。我没有IDEa意味着什么。

(IDE代表交互式开发环境,例如Eclipse中,NetBeans的,IDEA等 SO代表堆栈溢出。我指的是简单的wiki语言,SO提供写作和格式问题与解答。)

如果你还没有学会/被教了Java风格的规则,你应该花时间来阅读本文件 - Code Conventions for the Java TM Programming Language。还有其他的Java风格指南也是...请选择。良好风格的关键之一是一致地缩进代码。

原因样式约定是为了更容易阅读你自己和其他人的代码。并且要避免同事们把炸弹扔进你的隔间。

(如果你认为我是你艰难的,只是等待,直到你遇到你的第一个完整的代码审查的工作环境......)

+0

谢谢,我已经使用了大部分内容,但是关于stule的东西是正确的。 我的工作组显示器实际上也很惊讶,我们从来没有向我们解释过的样式。 – Rdhao

0

一个环到另一个问题是,大多数我们忘记重新使用我们用来做循环的变量。我建议你到每一个循环结束后,reinicialize你可以改变的变量,有时是innecesary一步,但其良好的例行...

由于稔说,问题是,y永远不会重新初始化...

0

我不能确定代码是什么,但我看到检查方法需要输入一个单词参数,但它不使用它。 Pehraps你用词语来代替单词吗?

0

一个调试程序说得很清楚:当它在字“ma”上执行时,索引y的值为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; 
    } 
}