-1

I am getting a list of words as input from the user and storing their input in a String[] array. I then create a char[][] array called letterGrid which is filled with random letters and the words provided by the user. The user then has to enter a word that they want to find when the letterGrid is displayed on the console screen. The program will then check if the the entered word appears horizontally, vertically, or diagonally, and it will print out its location. I have the checkHorizontal and checkDiagonal methods working fine, but I am having a problem at checkVertical. For example, my sample input of words wasred, orange, yellow, green, blue, purple, rainbow, andcolours.` Down below is the output grid:

Word Search Error - Console Screen

As you can see, when I type in yellow (6 letters in length), the program outputs the location of the word, but then there is an out of bound error.

EDITED CODE

Here is the rest of the code upon the request of @Igor Khvostenkov:

 
  private String word; // This variable will be the user`s input when they chose to search for a word they have entered
    private int rowLocation; // This variable will represent the row number in which the word is at
    private int colLocation; // This variable will represent the column number in which the word is at
 
 // Create a method to compare the user`s word to the elements in the letter grid
 public void compare (String word) {
     
        for (int row = 0; row < letterGrid.length - 1; row++) {
         
            for (int col = 0; col < letterGrid[row].length - 1; col++) {
             
                if (letterGrid[row][col] == word.charAt(0)) {

                    rowLocation = row;
                    colLocation = col;

                    wordContains(); // Call on method to see if the word entered by the user appears horizontally, vertically, or diagonally
                    
                }//end of if
                
            }//end of inner for loop
            
        }//end of outer for loop
        
    }//end of compare(word)
    
    // Create a method that will check the direction of the user`s word input

    public void wordContains() {
     
        checkHorizontal(); // Checking if the word appears horizontally
        checkVertical(); // Checking id the word appears vertically
        checkDiagonal(); // Checking if the word appears diagonally
        
    }//end of wordContains()
    
    // Create a method to check if the user`s word appears HORIZONTALLY in the letter grid

    public void checkHorizontal() {
     
        for (int i = 1; i < (word.length()); i++) {
         
            if (colLocation + i > letterGrid[0].length - 1) {
             
                return;
                
            } else if(letterGrid[rowLocation][colLocation + i] != word.charAt(i)) {
             
               return;
               
            }//end of if..else if
            
        }//end of for loop
        
        System.out.println(word + " found horizontally at row " + rowLocation + " and column " + colLocation); // Word found!!
        System.out.println();

        return;

    }//end of checkHorizontal()

    // Create a method to check if the user`s word appears VERTICALLY in the letter grid
    
    public void checkVertical() {
    
        for (int i = 1; i < (word.length()); i++) {
        
            if (rowLocation + i > letterGrid.length - 1 && colLocation + i > letterGrid[0].length) {
                
             return;
             
            } else if (letterGrid[rowLocation + i][colLocation] != word.charAt(i)) {
             
             return;
             
            }//end of if..else if
            
        }//end of for loop
        
        System.out.println(word + " found vertically at row " + rowLocation + " and column " + colLocation); // Word found!!
        System.out.println();          
        
    }//end of checkVertical()
    
    // Create a method to check if the user`s word appears DIAGONALLY in the letter grid

    public void checkDiagonal() {
     
        for (int i = 1; i < (word.length()); i++) {
         
            if (colLocation + i > letterGrid[0].length - 1 || rowLocation + i > letterGrid.length - 1) {
             
                return;
                
            } else if (letterGrid[rowLocation + i][colLocation + i] != word.charAt(i)) {
             
                return;
                
            }//end of if..else if
            
        }//end of for loop
        
        System.out.println(word + " found diagonally at row " + rowLocation + " and column " + colLocation); // Word found!!
        System.out.println("");
        
    }//end of checkDiagonal()

I cant seem to know why this is happening, and how I can fix this. I am not that familiar with ArrayIndexOutofBounds Exceptions as I barely go through them, but recently I have been and Ive been trying to understand the problem and look for ways to help me solve it.

  • 1
    Its very likely that you can fix it yourself by using a debugger. If you are not familiar with the usage i would recommend you to look into this post: https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems – M.Dan Jan 22 '19 at 16:31
  • I have looked it before I`m pretty sure, and I am familiar with debugging, but for some reason, I still am unsure of how to fix my problem. – Yashvi Shah Jan 22 '19 at 16:33
  • 1
    You know that you can look at the value of each variable in each step? – M.Dan Jan 22 '19 at 16:34
  • Yes, I am aware, but the thing is that I don`t decide where the words will be placed or how they will be placed in the letterGrid, so mostly the words just appear diagonally or horizontally. It would take a lot of constant debugging, and as much as I would like to do so, I can`t because I`m low on time and I have to present this program. – Yashvi Shah Jan 22 '19 at 16:40
  • 1
    Could you please share the full code version of the program? Looks like `checkVertical()` called 2nd time and the second time `rowLocation` or `colLocation` are set out of array bounds or `rowLocation` is set to the bound and then you add 1 setting out of bound. – Igor Khvostenkov Jan 22 '19 at 21:07
  • I have updated my code upon your request – Yashvi Shah Jan 22 '19 at 21:52

1 Answers1

1

The problem in the code is in your if-condition in checkVertical() that the row and column could be either first or last, but you should check row or column. Your example with yellow failed due to first code find Y in the first row and second column and then continue scan and finally, it finds Y in the last row and checks rowLocation + i > letterGrid.length - 1 && colLocation + i > letterGrid[0].length which skipped, then calls else which adds 1 to the row, and as a result out of bound the array. This should work:

 public void checkVertical() {

        for (int i = 1; i < (word.length()); i++) {

            if (rowLocation + i > letterGrid.length - 1 || colLocation + i > letterGrid[0].length) {

                return;

            } else if (letterGrid[rowLocation + i][colLocation] != word.charAt(i)) {

                return;

            }//end of if..else if

        }//end of for loop

        System.out.println(word + " found vertically at row " + rowLocation + " and column " + colLocation); // Word found!!
        System.out.println();

    }//end of checkVertical()
Igor Khvostenkov
  • 714
  • 5
  • 15