Is there a function I can use to search character string?

时间:2019-04-16 23:59:35

标签: java string search

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!");

0 个答案:

没有答案