[英]What's Wrong With My Load Data Method?
这是我用来从文件加载保存的游戏数据的方法。 因此,我有时会收到称为error15的东西。 Google上出现的唯一一件事是与http有关,但那不是因为我没有这样做。 当对象将字符串打印到控制台时,它会完成保存数据的第一行,但不会继续读取其他行。 我有预感,这可能与我使用in.nextLine();
(还有其他我应该使用的东西吗?)如果有人可以告诉我我做错了什么,我将永远爱你。
/**
* 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
}
由于您尝试访问循环中的data[i][15]
(不存在),因此,您得到的15的错误(应该已经发布了BTW,并会导致立即得到答复)可能是ArrayIndexOutOfBoundsException
。
for (int i=0; i<15; i++){
for (int j=0; j<=15; j++){
当将data
变量初始化为new String[15][15]
应调整j
循环以匹配i
循环。 因此,将其转换为以下内容,一切都会起作用
for (int j=0; j<15; j++){
请注意<
而不是<=
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.