在一个文件中读取创建一个二维数组字搜索拼图并解决

时间:2016-02-10 07:57:30

标签: java arrays multidimensional-array bufferedreader filereader

好的,对于这个项目,我有一个文本文件,格式如下:

4 4
boba
cduf
engt
tach
aunt
bob
cat

第一行指定为单词搜索拼图的尺寸。我创建了一个这个大小的二维char数组来控制拼图。然后,我需要从接下来的4行读取每个char,在本例中,读入我的数组。在那之后,我必须通过搜索我创建的拼图之下的任何单词来解决这个难题,在这种情况下将是阿姨,鲍勃和猫。

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.LinkedList;

public class WordPuzzleSolver {
    private char[][] puzzle;
    private String word;
    private LinkedList<String> words;
    private int index = 0;

    public void readFile(File inFile) {
        try{
            FileReader read = new FileReader(inFile);
            BufferedReader reader = new BufferedReader(read);
            String[] dimensions = reader.readLine().split(" ");

            puzzle = new char[Integer.parseInt(dimensions[0])][Integer.parseInt(dimensions[1])];

            for(int i = 0; i < puzzle[0].length; i++){
                String val = reader.readLine();
                puzzle[i] = val.toCharArray();
            }
            words = new LinkedList<String>();
            word = reader.readLine();
            while(word != null){
                words.add(word);
                word = reader.readLine();
            }

            while(index < words.size()){
                solvePuzzle();
                index++;
            }
            reader.close();
        }catch(IOException e){
            e.printStackTrace();
        }
    }

    public void solvePuzzle() {
        char firstLetter = words.get(index).charAt(0);

        for(int i = 0; i < puzzle.length; i++){
            for(int j = 0; j < puzzle[i].length; j++){

                if(puzzle[i][j] == firstLetter){

                    if(checkUp(i, j, words.get(index))){
                        print(words.get(index), i, j, "U");

                        //System.out.println("the word found:"+word);
                    }
                }
            }
        }
    }

    private boolean checkUp(int i, int j, String word){

        int index = j;
        for(char letter : word.toCharArray()){

            if(index >= puzzle[i].length){
                return false;
            }

            if(puzzle[i][index] != letter){
                return false;
            }

            index++;
        }
        return true;
    }

我在阅读我的文件时做错了,但我无法弄清楚是什么。因为现在当我尝试使用char firstLetter = word.charAt(0);方法拨打solvePuzzle()时,我收到了NullPointerException。这是我的主要问题,但在我开始工作后,我不确定我的checkUp方法是否写得正确。我仍然需要为其他7个可能的方向编写辅助方法,但我想首先在文本文件中使用这个方法,其中唯一要解决的词就是这个方向。

这是我得到的当前堆栈跟踪:

Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
    at java.util.LinkedList.checkElementIndex(Unknown Source)
    at java.util.LinkedList.get(Unknown Source)
    at WordPuzzleSolver.solvePuzzle(WordPuzzleSolver.java:43)
    at WordPuzzle.solve(WordPuzzle.java:77)
    at WordPuzzle.actionPerformed(WordPuzzle.java:95)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$500(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

在抛出错误之前调用的最后一行是当我调用char firstLetter = words.get(index).charAt(0);

1 个答案:

答案 0 :(得分:1)

我已经处理了你的代码。 以下是工作代码

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

public class WordPuzzleSolver {
    private static  char[][] puzzle;
    public static String word;

    public static void readFile(File inFile) {
        try{
            FileReader read = new FileReader(inFile);
            BufferedReader reader = new BufferedReader(read);
            String[] dimensions = reader.readLine().split(" ");

            puzzle = new char[Integer.parseInt(dimensions[0])][Integer.parseInt(dimensions[1])];

            for(int i = 0; i < puzzle[0].length; i++){
                String val = reader.readLine();
                puzzle[i] = val.toCharArray();
            }
            word = reader.readLine();
            while(word != null){
                solvePuzzle();
                word = reader.readLine();
            }
            reader.close();
        }catch(IOException e){
            e.printStackTrace();
        }
    }

    public static void solvePuzzle() {
        char firstLetter = word.charAt(0);

        for(int i = 0; i < puzzle.length; i++){
            for(int j = 0; j < puzzle[i].length; j++){

                if(puzzle[i][j] == firstLetter){

                    if(checkUp(i, j, word)){
                        print(word, i, j, "U");

                        //System.out.println("the word found:"+word);
                    }
                }
            }
        }
    }

    private static boolean checkUp(int i, int j, String word){

        int index = j;
        for(char letter : word.toCharArray()){

            if(index >= puzzle[i].length){
                return false;
            }

            if(puzzle[i][index] != letter){
                return false;
            }

            index++;
        }
        return true;
    }