Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8801207
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:46:35+00:00 2026-06-14T00:46:35+00:00

I know that this is quite a long and probably confusing post… but I

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T00:46:37+00:00Added an answer on June 14, 2026 at 12:46 am

    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:

      for(int L = 0; L < x; L++)
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I researched for this quite a long time, but could not find proper answer.
I know that this sort of question has been asked here before, but still
I know that this question must have been asked and answered a million times,
I know that this is a newbie question but I am a newbie so
I know that this has already been asked here but the answer (using a
I know that this feature will be deprecated in C++0x, but for me as
This is quite a long introduction to a simple question, but otherwise there will
This is quite a long shot, I know it's a question about a very
I have been struggling with this problem for quite a long time now. I
To begin, I must say that I have searched for quite a long time

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.