【问题标题】:Reading in a file to create a 2d array word search puzzle and solve读取文件以创建二维数组单词搜索难题并解决
【发布时间】:2016-02-10 07:57:29
【问题描述】:

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

4 4
boba
cduf
engt
tach
aunt
bob
cat

第一行指定了单词搜索谜题的尺寸。我创建了一个该大小的 2d 字符数组来保存拼图。然后,我需要将接下来 4 行中的每个字符(在这种情况下)读取到我的数组中。之后,我必须通过搜索我创建的拼图下方的任何单词来解决拼图,在本例中是 aunt、bob 和 cat。

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;
    }

我在读取文件时做错了,但我不知道是什么。因为现在当我尝试在我的solvePuzzle() 方法中调用char firstLetter = word.charAt(0); 时,我得到一个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);

【问题讨论】:

  • 您能否发布堆栈跟踪,因为据我所知,当您在 while 循环中检查 word 不为空时,char firstLetter = word.charAt(0); 给出 NullPointerException 有点奇怪。 (另外,我在您的代码中发现了一个不相关的错误:for(int i = 0; i &lt; puzzle[i].length - 1; i++){ 应该是 for(int i = 0; i &lt; puzzle.length; i++){(所以 puzzle.length 而不是 puzzle[i].length;并且没有 - 1,因为您需要索引 0、1、2 和 3(四个字); 而不是 0、1 和 2。) 不过,这与您的 NullPointerException 无关..
  • 我将上面的代码编辑为下面发布的代码,并与堆栈队列一起进行了一些小的更改,因为我仍然收到此错误并且无法弄清楚原因。我一直在打印我的单词链接列表,以确保它被适当的单词填充,它一直是,所以我不知道为什么我仍然得到空指针异常。

标签: java arrays multidimensional-array bufferedreader filereader


【解决方案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;
    }

【讨论】:

  • 这对我有用。如果您无法获得解决方案,请更新
  • 好的,所以我做了这些更改,并添加了我自己的一些更改。我在这个程序中不能有静态方法/变量。我仍然发生同样的错误。我还添加了堆栈队列以及引发空指针异常的代码行。
  • @DavidDoll。这不是NullPointerException。这是IndexOutOfBoundsException。而且,是的,这很重要。
  • @MadPhysicist 呜呼这就是我想说的。无论如何,我不明白为什么我会得到那个。在我的测试文件中,我只有一个要搜索的单词,它会被设置为链表中的第 0 个索引,并且我的索引变量被初始化为 0。
  • @DavidDoll。 #1,int index = j; 是一件非常糟糕的事情。 #2,你没有显示完整的代码,所以我怀疑错误可能在其他地方。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多