简体   繁体   中英

Reading in a file to create a 2d array word search puzzle and solve

Ok, so for this project I have a text file that is in the following format:

4 4
boba
cduf
engt
tach
aunt
bob
cat

The first line is designated to the dimensions of the word search puzzle. I create a 2d char array of that size to hold the puzzle. Then, I need to read each char from the next 4 rows, in this case, into my array. After that I must solve the puzzle by searching for any words that are below the puzzle I created, which in this case would be aunt, bob, and 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;
    }

I'm doing something wrong with reading in my file, but i cannot figure out what. Because right now when i try to call char firstLetter = word.charAt(0); in my solvePuzzle() method i am getting a NullPointerException . That is my main problem but after i get that working I am unsure if my checkUp method is written correctly. I still have to write helper methods for the other 7 possible directions, but I wanted to use this one first on a text file where the only word to solve is in this direction.

Here is the current stack trace that I get:

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)

the last line that is called before the error is thrown is when i call char firstLetter = words.get(index).charAt(0);

I have worked on your code. Below is the working code

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

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