javafilereaderwordsearch

Working With Puzzle and figuring how to fix out of bound error


My error Descriptions
Im supposed to read from a file a word search puzzle my problem I keep getting getting out of bound error. I don't know how to properly format a word puzzle to the file's specification. the format of a puzzle generally goes likes this: 5 5
d e v o l
r e d p h
q c h z j
p o a a f
v a m m n
q t f o x

This is my work so far, I feel that I have reading from a file down but converting that to a word search puzzle. Especially trying to not hard code the specification of the row and column of a word seaarch puzzle.

public static char[][] fill(){
    // Created 2 different scanner one for user input and one to read the file

    Scanner file1 = new Scanner(System.in);
    // created a count to add the keywords
    int count = 0;

    //System.out.print("Please enter a keyword to search for.");
    // Asking user to input a valid file name
    System.out.print("Please enter a valid puzzle file name\nYou will be asked for the same file name again later.");
    String wordFile = file1.nextLine();
    FileReader infile;
    boolean validFile = false;
    // Creating a while loop that will keep asking for a valid file name
    while(!validFile) {
        // Using a try and catch to obtain correct file
        try {
            infile = new FileReader(wordFile);
            file1 = new Scanner(infile);
            validFile = true;
        }
        //ask the user to put a valid file name if they are wrong
        catch(IOException e) {
            System.out.println("Not a valid file name, please enter again!");
            wordFile = file1.nextLine();
        }
    }
    String numbers = file1.nextLine();
    String[] fileArray = numbers.trim().split(" ");

    int rows = Integer.parseInt(fileArray[0]);
    int columns = Integer.parseInt(fileArray[1]);

    char[][] fillThemLetters = new char [rows][columns];


    String letters = file1.nextLine().trim().replace(" ", "");

    char [] condensed =  letters.toCharArray(); 

    for (int i = 0; i < condensed.length; i++) {

        for(int row = 0; row < rows; row++){


            for(int column = 0; column < columns; column++)
            {
                char index = condensed[i];
                fillThemLetters[row][column] = index;
                i++;
            }
        }

    }   
    return fillThemLetters;
}

Solution

  • Your index out of bounds error is caused here:

    for(int column = 0; column < columns; column++)
    {
        char index = condensed[i];
        fillThemLetters[row][column] = index;
        i++; // <--------- THIS IS WRONG!!!
    }
    

    See that i++? You're incrementing the counter from the outermost loop for no apparent reason. Let the loop handle its own incrementation, which is built in already, like so:

    for (int i = 0; i < condensed.length; i++) {  // <--- You already have i++ here!
    

    After fixing that, you're going to run into a lot more problems -- your code isn't doing what you think it's doing, but those are all separate questions and should be posted as such separately.