[英]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.