我试图加载一个2d int数组,我加载到一个文件在我的代码的前一个方法。当我到达LoadFromFile方法时,我似乎无法理解如何将其实现回我的Course类并将其添加回类中声明的Map。这是我现在用我的教授给我们的指示和我试图弄清楚的代码。在这一点上,我几乎是在做一些无意义的东西,希望它能触发一些东西来让这个工作。
提前感谢任何帮助或指导!
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
import java.util.ArrayList;
import java.awt.Point;
import java.awt.Dimension;
public class TileMap implements Loadable
{
private Dimension Size;
private int[][] Map;
public TileMap(int NewWidth, int NewHeight)
{
Size = new Dimension(NewWidth, NewHeight);
Map = new int[NewWidth][NewHeight];
}
// accessors and mutators for Size
public Dimension GetSize()
{
return Size;
}
public void SetSize(int NewWidth, int NewHeight)
{
Size = new Dimension(NewWidth, NewHeight);
Map = new int[NewWidth][NewHeight];
}
// accessor and mutator for Map
public int[][] GetMap()
{
return Map;
}
public void SetMap(int[][] NewMap)
{
// copy the reference
Map = NewMap;
}
/**
* Loads a TileMap from a file. The first line of the file will have the width, the
* first dimension of the array. The second line will have the height, the second
* dimension of the array. Then the values in the array should follow as demonstrated
* below.
*
* Example:
* 6n
* 6n
* 2,1,1,0,0,2n
* 0,0,1,0,0,1n
* 0,0,1,0,0,1n
* 0,2,1,0,0,1n
* 0,0,1,1,1,1n
* 0,0,1,0,0,1n
*
* @param Filename the file to load from
* @return the empty string "" if the load succeeds, or the exception message if it fails
*/
public String LoadFromFile(String Filename)
{
try
{
File nf = new File(Filename);
Scanner in = new Scanner(nf);
String text = "";
int x =0;
//load file and assign all scans
//to respected properties within class
while(in.hasNextLine())
{
text = in.nextLine() + "n";
}
int w = Integer.parseInt(text[0]);
int h = Integer.parseInt(text[1]);
SetSize(w,h);
int[][] newMap = null;
int k = 2;
for(int i = 0; i < GetSize().width;i++)
{
for(int j = 0; j < GetSize().height; j++)
{
newMap[i][j] = Integer.parseInt(text[k]);
k++;
}
}
SetMap(newMap);
in.close();
}
catch(FileNotFoundException e)
{
return Filename + " (No such file or directory)";
}
catch(Exception f)
{
f.getStackTrace();
}
return "";
}
我看到你在cs49j。我们是这样做的
public String LoadFromFile(String Filename)
{
// YOUR CODE HERE
try
{
File inFile = new File(Filename);
Scanner inText = new Scanner(inFile);
inText.useDelimiter("[\s,rn]+");
int tempW = inText.nextInt();
int tempH = inText.nextInt();
SetSize(tempW, tempH);
for (int i = 0; i < tempW; i++)
{
for (int j = 0; j < tempH; j++)
{
if (inText.hasNextInt())
{
int temp = inText.nextInt();
Map[i][j] = temp;
}
}
}
inText.close();
return "";
}
catch (Exception e)
{
String dir = " (No such file or directory)";
return Filename + dir;
}
}
一定要检查你的地图。
这很简单,而且你的方向是正确的。
你读这两行,给它们分配宽度和高度,这是你已经做过的。
你做错的是,你不应该一次读取整个文件,读取前两行,将它们解析成数字。你会知道你需要多读多少行(它是w)
如果你知道有多少行,你必须做以下w次:读下一行将下一行解析(用逗号分隔)为字符串数组(java将使用string.split完成此操作)遍历字符串数组,将每个变量转换为数字,然后将它们放入相应的数组元素
如果你的newMap是null,你应该将它初始化为new int[w][h]
如果文件中的值看起来像你的评论:
* Example:
* 6n
* 6n
* 2,1,1,0,0,2n
* 0,0,1,0,0,1n
* 0,0,1,0,0,1n
* 0,2,1,0,0,1n
* 0,0,1,1,1,1n
* 0,0,1,0,0,1n
你应该使用text变量作为字符串数组。这样做的结果是,文本变量只包含文件的最后一行。
String[] text = new String[];
int x = 0;
while(in.hasNextLine())
{
text[x++] = in.nextLine();
}
int w = Integer.parseInt(text[0]);
int h = Integer.parseInt(text[1]);
那么你对上面的2行也有问题。每行包含以逗号分隔的数字,因此必须用逗号分隔符分隔它们,然后可以将每个值分配给newMap的正确维度:
for (int i = 2; i <= text.length; i++) {
String[] temp = text[i].split(",");
for (int j = 0; j <= temp.length-1, j++) { //temp.length-1 is because the last string will be a number + "n"
newMap[i-2][j] = Integer.parseInt(temp[j]);
}
newMap[i-2][temp.length] = Integer.parseInt(temp[temp.length].substring(0,1)); //taking care of the last string of the row
}
你不应该将newMap指向null,而应该使用
int[][] newMap = new int[w][h];