English 中文(简体)
j语节目
原标题:Wordhunt program in java
  • 时间:2011-03-14 09:36:52
  •  标签:
  • java

This is what it do ... I have a .txt file that i have stored in a 2d Array and it works ... heres the problem im supposed to search for a word in the table and i can t figure out a way for me to compare the letters in the arrays and return it s index.... (so basically i want to find a letter in the 2d array and i want it to return it s index int c[][]) the real assignment was like a wordhunt puzzle ( where it can find it horizontal vertical diagonal (left and right) aswell as backwards ( in the said directions) but i m pretty sure i can get around it once i know how i can get the indexes of the 2st letter of the word i search for

import java.io.*;
import java.util.Scanner;


public class Mp6 {


public static void main(String[] args) throws IOException {

    FileReader a = new FileReader("data/Data.txt");
    BufferedReader ss = new BufferedReader(a);
    char wla[][];
     int x = 80;
     int y = 80;
    wla = new char [x][y];
    String d;
    Scanner p = new Scanner(System.in);
    int c = 0;
    int n = 0 ; 


    try {
        for( c = 0; c < x; c++){ 
                d = ss.readLine(); 
        for ( n = 0; n<y;n++){
                wla[c][n]=d.charAt(n);
                System.out.print(wla[c][n]); 
                iii
                 System.out.println("");
            iii

        System.out.println("Search for:");
         String h = p.nextLine();
         h=h.toUpperCase();
         char [] ph = new char[h.length()];
         int counter = 0;
         for(counter = 0 ; counter < h.length();counter++){
             ph[counter]=h.charAt(counter);
             System.out.print(ph[counter]);
         iii
         //horizontal


    iii catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    iii 
iii

iii

问题回答

如果我理解你的话,你就希望从所有方向在2个阵列中找到这个词。 在此情况下,最佳算法应当做到:

  1. Loop through the array finding the first letter of the word.
  2. After finding one - search in all directions for remaining letters.

这是如何做到的样本:

char[] word = getWordToFind();
char[][] array = getArrayWhereToSearch();

//Search for first letter of the word
for (int x = 0; x<array.length;x++) {
    for (int y=0; y<array[x].length;y++) {
        //If the first letter is found
        if (array[x][y] == word[0]) {
            //Search in all directions
            if (searchEast(array, word, x, y)) {
                println(String.format("Word match found! X: %d, Y: %d, direction:  %s ", x, y, "east"));
            }
            if (searchSouth(array, word, x, y)) {
                println(String.format("Word match found! X: %d, Y: %d, direction:  %s ", x, y, "south"));
            }
            //More directions if needed
            ...
        }
    }
}

public boolean searchEast(char[][] array, char[] word, int startX, int startY) {
    //If the word can t fit to the right
    if (x >= array.length-word.length) {
        return false;
    }

    //Search the remaining letters to the right starting from startX+1
    for (int dx = 1; dx<word.length;dx++) {
        if (array[startX+dx] != word[dx]) { //If the letter mismatches
            return false;
        }
    }

    //All letters matched
    return true;
}

public boolean searchSouth(char[][] array, char[] word, int startX, int startY) {
    //If the word can t fit to the bottom
    if (y >= array[startX].length-word.length) {
        return false;
    }

    //Search the remaining letters to the bottom starting from startY+1
    for (int dy = 1; dy<word.length;dy++) {
        if (array[startX][startY+dy] != word[dy]) { //If the letter mismatches
            return false;
        }
    }

    //All letters matched
    return true;
}




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签