简体   繁体   English

我的负载数据方法有什么问题?

[英]What's Wrong With My Load Data Method?

This is a method I'm using to load saved game data from a file. 这是我用来从文件加载保存的游戏数据的方法。 I'm getting something called error15 at some point because of this. 因此,我有时会收到称为error15的东西。 The only thing that came up on Google was something to do with http, but that can't be it because I'm not doing anything like that. Google上出现的唯一一件事是与http有关,但那不是因为我没有这样做。 When the object prints the strings to the console, it completes the first line of saved data but doesn't go on to read the others. 当对象将字符串打印到控制台时,它会完成保存数据的第一行,但不会继续读取其他行。 I have a hunch it might have to do with my use of in.nextLine(); 我有预感,这可能与我使用in.nextLine(); (is there something else I should be using instead?) If anyone can tell me what I'm doing wrong I will love you forever. (还有其他我应该使用的东西吗?)如果有人可以告诉我我做错了什么,我将永远爱你。

/**
 * Returns a 2-dimensional 15*15 array of saved world data from a file named by x and y coordinates
 */
public String[][] readChunkTerrain(int x, int y)
{
    String[][] data = new String[15][15]; //the array we will use to store the variables
    try {
        //initiate the scanner that will give us information about the file
        File chunk = new File( "World/" + x + "." + y + ".txt" );
        Scanner in = new Scanner(
                new BufferedReader(
                    new FileReader(chunk)));

        //go through the text file and save the strings for later
        for (int i=0; i<15; i++){
            for (int j=0; j<=15; j++){
                String next = in.next();
                data[i][j] = next;

                System.out.println(i + j + next); //temporary so I can see the output in console
                System.out.println();
            }
            in.nextLine();
        }

        in.close(); //close the scanner
    }
    //standard exception junk
    catch (Exception e)
    {System.err.println("Error" + e.getMessage());}

    return data; //send the array back to whoever requested it
}

The error with the 15 you get (which you should have posted BTW, and would resulted in an immediate answer) is probably an ArrayIndexOutOfBoundsException since you try to access data[i][15] in your loop, which does not exists. 由于您尝试访问循环中的data[i][15] (不存在),因此,您得到的15的错误(应该已经发布了BTW,并会导致立即得到答复)可能是ArrayIndexOutOfBoundsException

    for (int i=0; i<15; i++){
        for (int j=0; j<=15; j++){

Your j loop should be adjusted to match the i loop as your data variable is initialized as new String[15][15] . 当将data变量初始化为new String[15][15]应调整j循环以匹配i循环。 So convert it to the following and all will work 因此,将其转换为以下内容,一切都会起作用

        for (int j=0; j<15; j++){

Note the < instead of <= 请注意<而不是<=

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

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