简体   繁体   English

使用 Puzzle 并弄清楚如何修复越界错误

[英]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拼图的格式通常是这样的:5 5
devol转移
redph红卫兵
qchzj知乎
poaaf农夫
vammn瓦姆恩
qtfox qtfox

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

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++ ?看到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.解决这个问题后,您将遇到更多问题——您的代码没有按照您认为的那样做,但这些都是单独的问题,应该单独发布。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM