1

Looking for some guidance on how would you implement a search function if the users input is in the grid or "board" in my case. For example, if user enters "bay" for input it prints back "Word: bay Exists at column1row5:column3row5. So letter "b" being the first column and being the fifth row, the last letter being the third column and so on. So the location starts on the first letter and ends on the last letter of word found. Would I do a string indexOf()???

I've tried indexOf(), but think I'm implementing it the wrong way.

public static void main(String[] args) {

    char row1[] = {'t', 'm', 'e', 'r' };
    char row2[] = {'a', 'i', 'z', 'g' };
    char row3[] = {'k', 'l', 's', 'f' };
    char row4[] = {'e', 'e', 'o', 'r' };
    char row5[] = {'b', 'a', 'y', 'y' };

    char board[][] = new char[5][4];
    board[0] = row1;
    board[1] = row2;
    board[2] = row3;
    board[3] = row4;
    board[4] = row5;

    wordsearch puzzle = new wordsearch();
    System.out.println("Type in a word to search!: ");

    String input = scanner.nextLine();
    if (puzzle.wordExists(board, input.toString())) {
        System.out.println("Word: " + input + " Exists !");
   }else {
        System.out.println("Word Not Found!");
  • Possible duplicate of [Finding an element in an array in Java](https://stackoverflow.com/questions/3384203/finding-an-element-in-an-array-in-java) – GBlodgett Apr 17 '19 at 00:00
  • Can you share your WordSearch class including wordExists method here ? – royalghost Apr 17 '19 at 00:24
  • I'm trying to stick with One Dimensional Arrays, I get the idea indexOf(), but is there a way to implement it without using a for loop? I want the start and end, but based on users input. –  Apr 17 '19 at 00:25
  • Do I have to paste my whole code though? –  Apr 17 '19 at 00:47
  • Maybe helpful to have the `rows` as Strings, then searching would be easier. Conversion from String to char array is also easy – Scary Wombat Apr 17 '19 at 01:48
  • Is there a easy way to do it as I have it now? –  Apr 17 '19 at 01:59
  • Since it appears you can't use loops and just as a mere exercise try to do it with a [Recursive](https://www.cs.bgu.ac.il/~ipc192/wiki.files/Class_Java_6.pdf) method. See also [this SO post](https://stackoverflow.com/questions/5112749/java-array-recursion). – DevilsHnd - 退職した Apr 17 '19 at 02:38

0 Answers0