简体   繁体   中英

How do I compare a a string array with a char array

I am desperate and I need your help! I am working on a crossword puzzle solver where the puzzle and the words are given to us and we need to format the puzzle so each index in the array has it's own letter. Right now I have the puzzle index split and the words split so I can go in and compare them letter by letter but is there a better way? Is it possible to, lets say, have a string array that contains the word I'm looking for and have an array of chars locate that word?

    My array looks like this: 
    [W, V, E, R, T, I, C, A, L, L]
    [R, O, O, A, F, F, L, S, A, B]
    [A, C, R, I, L, I, A, T, O, A]
    [N, D, O, D, K, O, N, W, D, C]
    [D, R, K, E, S, O, O, D, D, K]
    [O, E, E, P, Z, E, G, L, I, W]
    [M, S, I, I, H, O, A, E, R, A]
    [A, L, R, K, R, R, I, R, E, R]
    [K, O, D, I, D, E, D, R, C, D]
    [H, E, L, W, S, L, E, U, T, H]

And lets say I'm looking for "Vertical". How can I set up a loop to look for the chars that make up the string "Vertical". Or do I have to compare it letter by letter?

This is my code:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;


public class WordSearch {

    public static void main(String[] args) throws Exception{
            File file = new File("puzzle.txt"); 
            Scanner sc = new Scanner(file);
        int row = sc.nextInt();
        int col = sc.nextInt();
        sc.nextLine();

        //Make an array to hold the puzzle
        char[][] puzzle = new char[row][col];   


         //Read in the strings from the file into the array.
        for (int i=0; i<row; i++){
            String getChar = (new String(sc.next()));
            for(int j = 0;j<col; j++){
                puzzle[i][j] = getChar.charAt(j);
                }
            }

        //Test Print
         for (int i=0; i<puzzle.length; i++){

               System.out.print(Arrays.toString(puzzle[i]));
               System.out.println("");
         }



        //Read the number of words and move to the next line    
        int numwords = sc.nextInt();
        sc.nextLine();

        //Make an array to hold the words and read in the words from a file
        String[] words = new String[numwords];
        for(int i=0; i<numwords; i++){
            words[i] = sc.nextLine();
        }

        //look for each word
        for(int i=0; i<numwords; i++){
            String currentword = words[i];      

            String[] strings1 = currentword.split("");

            int range = currentword.length()+1;

            String[] strings2 = Arrays.copyOfRange(strings1, 1, range);

            int range2 = strings2.length;
            char[] charwords = new char[range2];    





            }



    //Close the scanner     
    sc.close();
    }

    }

Would this work to test for the diagonals:

private boolean checkDiagonals(int row, int col, String word, char[][] puzzle) {
        //Checking diagonals direction
        for(int letter = 1; letter < word.length(); letter++) {
            if(puzzle[row + letter][col + letter] != word.charAt(letter)) {
                return false;
            }
            else if(puzzle[row + letter][col - letter] != word.charAt(letter)) {
                return false;
            }
            else if(puzzle[row - letter][col - letter] != word.charAt(letter)) {
                return false;
            }
            else if(puzzle[row - letter][col + letter] != word.charAt(letter)) {
                return false;
            }
        }
        return true;
    }

Is it possible to, lets say, have a string array that contains the word I'm looking for and have an array of chars locate that word?

Yes, Simply do

get element from String array like

  String arrayElem = str[i]; // i is index.

which gives you a string.

Then

  boolean result=Arrays.equals(arrayElem.toCharArray(), actualCharArray);

Because the words can be in any direction, it'd make more sense to simply go letter by letter. Search the entire array of characters for the first letter of the word you're looking for. For example, if you were looking for "VERTICAL", you would go through the whole array looking for the letter 'V'.

Then have a function that verifies the word for all directions (up, down, left, right, diagonals). Let's call it check(int row, int col) .

for(int row = 0; row < puzzle.length; row++) {
    for(int col = 0; col < puzzle[0].length; col++) {
        if(puzzle[row][col] == word.charAt(0))
            if(check(row, col) == true)
                    //We found it.
    }
}

Then to write check(int row, int col, String word, char[][] puzzle) , you'd check each cardinal direction, for example, if you were checking right, you'd keep going right and compare the letters to the word until you match the word, or find an inconsistency. Then if no direction works, return false.

As an example, here's checking to the right (no error checking in the case we're out of the array, you'd want to implement that).

private boolean checkRight(int row, int col, String word, char[][] puzzle) {
    //Checking right direction
    for(int letter = 1; letter < word.length(); letter++) {
        if(puzzle[row][col + letter] != word.charAt(letter)) {
            return false;
        }
    }
    return true;
}

You'd then want one of these for each direction, and then in your check function, it'd look something like

public boolean check(int row, int col, String word, char[][] puzzle) {
    if(checkRight(row, col, word, puzzle)) return true;
    if(checkLeft(row, col, word, puzzle)) return true;
    if(checkUp(row, col, word, puzzle)) return true;
    if(checkDown(row, col, word, puzzle)) return true;
    if(checkDiagonals(row, col, word, puzzle)) return true;
    return false;
}

I would suggest converting each row in the array to a String then use String.indexOf() . This will determine if the string you are looking for exists in the row and at what position it begins. If you get back -1 you know the string does not exist in the row.

String.indexOf()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM