Reputation: 107
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;
}
}
}
}
}
Upvotes: 0
Views: 921
Reputation: 33655
You don't reset y
in the inner loop, when the loop proceeds to the second word, y
is 3, but words[x].charAt(y)
(where x
= 1) doesn't exist - it's out of bounds.
Upvotes: 1
Reputation: 308733
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;
}
}
Upvotes: 0
Reputation: 718698
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 ... )
Upvotes: 0
Reputation: 1056
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?
Upvotes: 0
Reputation: 1234
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...
Upvotes: 0
Reputation: 10285
the 2nd word in the array words is only composed of 2 letters and you are trying to access the 4th letter in that word by the follwing code words[x].charAt(y) but y is equal to 3 which is out of bound for the word ma
Upvotes: 1
Reputation: 76
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
Upvotes: 1
Reputation: 10747
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.
Upvotes: 0