【发布时间】: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 < puzzle[i].length - 1; i++){应该是for(int i = 0; i < puzzle.length; i++){(所以puzzle.length而不是puzzle[i].length;并且没有- 1,因为您需要索引 0、1、2 和 3(四个字); 而不是 0、1 和 2。) 不过,这与您的 NullPointerException 无关.. -
我将上面的代码编辑为下面发布的代码,并与堆栈队列一起进行了一些小的更改,因为我仍然收到此错误并且无法弄清楚原因。我一直在打印我的单词链接列表,以确保它被适当的单词填充,它一直是,所以我不知道为什么我仍然得到空指针异常。
标签: java arrays multidimensional-array bufferedreader filereader