我正在尝试加载我在代码的先前方法中加载到文件中的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:
* 6\n
* 6\n
* 2,1,1,0,0,2\n
* 0,0,1,0,0,1\n
* 0,0,1,0,0,1\n
* 0,2,1,0,0,1\n
* 0,0,1,1,1,1\n
* 0,0,1,0,0,1\n
*
* @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 "";
}
答案 0 :(得分:0)
这很容易,你开始朝着正确的方向前进。
你读了两行,把它们分配给你已经完成的宽度和高度。
你做错了以下,你不应该一次读完整个文件,阅读前两行将它们解析成数字。你会知道你需要阅读多少行(这是w)
如果你知道将有多少行,你必须做以下w次: 阅读下一行 将下一行(在comas上拆分)解析为字符串数组(java将使用string.split执行此操作) 迭代字符串数组,将每个变量转换为数字,然后将它们放入相应的数组元素
你的newMap也是null,你应该把它初始化为new int [w] [h]
答案 1 :(得分:0)
如果您的评论中的文件中的值如下:
* Example:
* 6\n
* 6\n
* 2,1,1,0,0,2\n
* 0,0,1,0,0,1\n
* 0,0,1,0,0,1\n
* 0,2,1,0,0,1\n
* 0,0,1,1,1,1\n
* 0,0,1,0,0,1\n
你应该将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];
答案 2 :(得分:0)
我看到你在CS 49J。这是我们如何做到的,
public String LoadFromFile(String Filename)
{
// YOUR CODE HERE
try
{
File inFile = new File(Filename);
Scanner inText = new Scanner(inFile);
inText.useDelimiter("[\\s,\r\n]+");
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;
}
}
确保在方法调用之间检查Map.tm,以便在那里获得正确的信息。