I know that this is quite a long and probably confusing post… but I really have no idea what could be causing this… so i did the best I could at explaining. I am not sure how to describe the problem succinctly, which has made it rather difficult for me to look for other solutions.
Anyway, First a slight bit of background. This program is reading through a 2D array in all of a possible 8 directions looking for a word (The program is basically an automated word find).
I have the 2D array and I am searching through it, looking for a given string. To do this, I have 3 loops:
- The first loop (looping x) chooses the starting x-coordinate
- The second loop (looping y) chooses the starting y-coordinate
- The last loop (looping L) checks for a word of length L starting at the positions given by (x,y) coordinates from the outer loops.
However, It seems the program does not enter the loop for L until the outer loops have increased to 1. Because of this, I cannot start a search for a word in column 1 (x=0), the first one that gets accessed is column 2 (x=1). It is almost as if there is an if statement preventing the for loop from running when the values from either of the other two loops are below 1. What is more, this ONLY HAPPENS When I am subtracting index locations of the array, not when I am adding them.
To clarify on that last point, I am searching for words by adding chars to a string one at a time. So when reading forward, a three letter word ‘cat’ would consist of a string ‘c’ at location x, then we add to the string the char ‘a’ at location x+1, then we add to that string the char ‘t’ from location x+2. This basically gets writted as arr[x+L][y], to search forward for a word. HOWEVER, I also want to find words written backwards. so for that I have a loop the adds arr[x-L][y]. It seems that this error ONLY happens when I am subtracting L from the indicies in my array, and only happens to the value that it is subtracted from (so for [x-1][y], x=0 does not go into the for loop, but y=0 DOES.
Here is the (shortened) code:
public void searchPuzzle(String[][] Puzzle){
//For Each Coordinate X,Y
for(int x=0; x<Puzzle.length; x++){//For each x position
for(int y=0; y<Puzzle[x].length; y++){//For each y position
//RESET or INITAITE strings for searching in all 8 directions
String F="", B="", U="", D="", DiFU="", DiFD="", DiBU="", DiBD="";
//SEARCH FORWARD
for(int L = 0; L < Puzzle.length-x; L++){ //search for word of length L
F = F+Puzzle[x+L][y];
//System.out.println(F);
if(isWord(F)){
//System.out.println(F);
}
}
//SEARCH BACK
System.out.println("X:" + x + " Y: "+y);
for(int L = 0; L < x; L++){ //search for word of
System.out.println("X:" + x + " Y: "+y);
B = B+Puzzle[x-L][y];
System.out.println(B);
if(isWord(B)){
//System.out.println(B);
}
}
System.out.println("--");
//SEARCH DIAGONALLY - BACK & UP
for(int L = 0; L < smaller(x,y); L++){
DiBU = DiBU+Puzzle[x-L][y-L];
if(isWord(DiBU)){
//System.out.println(DiBU);
}
}
- SEARCH FORWARD
works perfectly (Because F+Puzzle[x+L][y] involves only addition).
So for this one the program to start reading at arr[0][0], as it should.
- SEARCH BACK
has the error I mentioned, but only the x value is wrong. This means that x=0 never gets passed into the loop SEARCH BACK, ans as a result the loop starts with x=1. Note that the code has B+Puzzle[x-L][y]
So for this one the program to start reading at arr[1][0].
- SEARCH DIAGONALLY – BACK & UP
has this error for both x AND Y. Because of “DiBU+Puzzle[x-L][y-L]”, BOTH x=0 and y=0 do not go into the loop. And so
So for this one the program to start reading at arr[1][1].
EDIT: I have tried simply subtracting 1 (as in B+Puzzle[x-L-1][y]), however this just causes it to start in the right place but end one column to short.
TL;DR
Innermost of three for loops cannot be read unless the value of the outer two loops is above 0. But this problem only occurs if I am subtracting values from the indices of an array inside the said innermost For Loop.
I think it is simpler to separate the forwards and backwards searches, at least until you get it all working. Do one thing at a time, and do it carefully.
The reason for not doing any iterations of the second loop if x is zero lies in the loop condition:
L is never less than 0, and so if x is 0 the condition L < x is never met, and the loop does zero iterations.
Suppose y is 0 and x is 2. The elements [2][0], [1][0], and [0][0] could be a three letter word. You will only do two iterations, for L=0 and L=1.
Both problems would be fixed by doing one more iteration by changing the condition to L <= x.
It is very, very important to think about your loop conditions. In many simple cases a “<” test is appropriate, but not always. In this case, L==x results in looking at element 0 of the array, which you want.