Find word in two dimensional char array Task: I have two dimensional array of chars filled with random values [a-z] and need to find if the word ala appears vertically, horizontally or diagonally in it.

In my program, I go through all characters and try to find word in all dimensions.

I feel there is more intelligent way to do this task which will:


Work with words of different length without hard-coding.
Replace bunch of if statements so it will be easier.


Any advice?

import java.util.Random;

public class Ala {
    public static void main(String[] args) {
        Random randomGenerator = new Random();
        char[][] arr = new char[50][50];
        for (int i = 0; i &lt; arr.length; i++) {
            for (int j = 0; j &lt; arr[i].length; j++) {
                arr[i][j] = (char) (randomGenerator.nextInt(26)+97);
            }
        }
        for (int i = 0; i &lt; arr.length; i++) {
                System.out.printf("%-3d", i);
        }
        System.out.println();
        for (int i = 0; i &lt; arr.length; i++) {
            for (int j = 0; j &lt; arr[i].length; j++) {
                System.out.print(arr[i][j]+"  ");
            }
            System.out.println();
        }
        boolean ala = false;
        for (int i = 0; i &lt; arr.length; i++) {
            for (int j = 0; j &lt; arr[i].length; j++) {
                if (j&gt;1) {
                    if (arr[i][j] == 'a' &amp;&amp; arr[i][j - 1] == 'l' &amp;&amp; arr[i][j - 2] == 'a') {//left
                        ala = true;
                        System.out.println(i + " " + j);
                    } 
                }
                if (j&lt;arr[i].length-2) {
                    if (arr[i][j] == 'a' &amp;&amp; arr[i][j + 1] == 'l' &amp;&amp; arr[i][j + 2] == 'a') {//rigth
                        ala = true;
                        System.out.println(i + " " + j);
                    } 
                }
                if (i&lt;arr.length-2) {
                    if (arr[i][j] == 'a' &amp;&amp; arr[i + 1][j] == 'l' &amp;&amp; arr[i + 2][j] == 'a') {//bot
                        ala = true;
                        System.out.println(i + " " + j);
                    } 
                }
                if (i&gt;1) {
                    if (arr[i][j] == 'a' &amp;&amp; arr[i-1][j] == 'l' &amp;&amp; arr[i - 2][j] == 'a') {//top
                        ala = true;
                        System.out.println(i + " " + j);
                    } 
                }
                if (i&lt;arr.length-2 &amp;&amp; j&lt;arr[i].length-2) {
                    if (arr[i][j] == 'a' &amp;&amp; arr[i + 1][j + 1] == 'l' &amp;&amp; arr[i + 2][j + 2] == 'a') {//bot&amp;rigth
                        ala = true;
                        System.out.println(i + " " + j);
                    } 
                }
                if (i&gt;1 &amp;&amp; j&gt;1) {
                    if (arr[i][j] == 'a' &amp;&amp; arr[i - 1][j - 1] == 'l' &amp;&amp; arr[i - 2][j - 2] == 'a') {//top&amp;left
                        ala = true;
                        System.out.println(i + " " + j);
                    } 
                }
                if (i&gt;1 &amp;&amp; j&lt;arr[i].length-2) {
                    if (arr[i][j] == 'a' &amp;&amp; arr[i - 1][j + 1] == 'l' &amp;&amp; arr[i - 2][j + 2] == 'a') {//top&amp;rigth
                        ala = true;
                        System.out.println(i + " " + j);
                    } 
                }
                if (i&lt;arr.length-2 &amp;&amp; j&gt;1) {
                    if (arr[i][j] == 'a' &amp;&amp; arr[i + 1][j - 1] == 'l' &amp;&amp; arr[i + 2][j - 2] == 'a') {//bot&amp;left
                        ala = true;
                        System.out.println(i + " " + j);
                    } 
                }
            }
        }
        System.out.println(ala);
    }
}
Your code is indeed sub-optimal.

When looking at a task do not start writing some loops, but think about it is made up of simpler tasks.

A word is in a two-dimensional crossword grid if any of the below is True:


The word is in any of the rows.
The word is in any of the columns.
The word is in any of the diagonals.


So, the general outline of the method may be:

class WordGrid {
  public contains(String word) {
    return any(word in row for row in getRows())                ||
           any(word in column for column in getColumns())       || 
           any(word in diagonal for diagonal in getDiagonals())
   }
}


Now you just have to translate this pseudocode to real code and implement the three get methods and the problem will be solved.

This is my suggestion because:


The general idea can be understood at a glance, if the reader wants more details he can read the helper methods.
It is made of many small pieces, that may be reused and are simpler to write on their own.
It is general working, for any grid and word size.