[英]String index out of bounds error, cant find the root of the 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)
我知道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;
}
}
}
}
}
您无需在内部循环中重置y
,当循环继续到第二个单词时, y
为3,但是words[x].charAt(y)
(其中x
= 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;
核实
数组单词中的第二个单词仅由2个字母组成,并且您尝试通过以下代码word [x] .charAt(y)访问该单词中的第四个字母,但y等于3 ,对于马这个词
对于某些单词 ,由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以获得预期的行为。
您可以通过调试程序来分析程序,以找出此类错误。
对不起,我真的不知道您想告诉我什么。 我的代码看起来有些混乱,是的,但是那没有人教过我该怎么做。 至于SO或IDE。 我没有任何想法。
(IDE代表交互式开发环境。例如Eclipse,NetBeans,IDEA等。SO代表堆栈溢出。我指的是SO提供的用于编写和格式化问题和答案的简单Wiki语言。)
如果您还没有学习过Java风格的规则,则应该花些时间阅读本文档-Java TM编程语言的代码约定 。 还有其他Java样式指南...请选择。 好的样式的关键点之一是使代码一致地缩进。
之所以采用样式约定,是为了使其更易于阅读您自己和他人的代码。 并避免同事将臭味炸弹刺入您的小隔间的情况。
(如果您认为我对您很苛刻,请等到您在工作场所环境中经历第一次全面的代码审查时……)
一个循环到另一个循环的问题在于,大多数时候我们忘记重新初始化用于循环的变量。 我建议您在每个循环之后重新初始化可能更改的变量,有时这是一个不必要的步骤,但它是一个很好的例程...
就像Nim所说的那样,问题是y
从未被初始化过...
我无法弄清楚代码的用途,但是我看到check方法接受了输入的单词param,但是它没有使用它。 Pehraps您用单词代替单词了吗?
调试器清楚地表明:对单词“ 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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.