For homework, we have to read in a txt file which contains a map. With the map we are supposed to read in its contents and place them into a two dimensional array.
对于家庭作业,我们必须读入包含地图的txt文件。使用地图,我们应该读取其内容并将它们放入二维数组中。
I've managed to read the file into a one dimensional String ArrayList, but the problem I am having is with converting that into a two dimensional char array.
我已经设法将文件读入一维String ArrayList,但我遇到的问题是将其转换为二维char数组。
This is what I have so far in the constructor:
这是我到目前为止在构造函数中所拥有的:
try{
Scanner file=new Scanner (new File(filename));
while(file.hasNextLine()){
ArrayList<String> lines= new ArrayList<String>();
String line= file.nextLine();
lines.add(line);
map=new char[lines.size()][];
}
}
catch (IOException e){
System.out.println("IOException");
}
When I print out the lines.size() it prints out 1 but when I look at the file it has 10.
当我打印出lines.size()时,它打印出1,但是当我查看文件时它有10个。
Thanks in advance.
提前致谢。
2 个解决方案
#1
4
You have to create the list outside the loop. With your actual implementation, you create a new list for each new line, so it will always have size 1.
您必须在循环外创建列表。通过实际实现,您可以为每个新行创建一个新列表,因此它的大小始终为1。
// ...
Scanner file = new Scanner(new File(filename));
List<String> lines = new ArrayList<String>(); // <- declare lines as List
while(file.hasNextLine()) {
// ...
BTW - I wouldn't name the char[][]
variable map
. A Map is a totally different data structure. This is an array, and if you create in inside the loop, then you may encounter the same problems like you have with the list. But now you should know a quick fix ;)
BTW - 我不会命名char [] []变量映射。 Map是一种完全不同的数据结构。这是一个数组,如果你在循环内创建,那么你可能会遇到与列表一样的问题。但现在你应该知道一个快速修复;)
#2
0
Change the code as following:
更改代码如下:
public static void main(String[] args) {
char[][] map = null;
try {
Scanner file = new Scanner(new File("textfile.txt"));
ArrayList<String> lines = new ArrayList<String>();
while (file.hasNextLine()) {
String line = file.nextLine();
lines.add(line);
}
map = new char[lines.size()][];
} catch (IOException e) {
System.out.println("IOException");
}
System.out.println(map.length);
}
#1
4
You have to create the list outside the loop. With your actual implementation, you create a new list for each new line, so it will always have size 1.
您必须在循环外创建列表。通过实际实现,您可以为每个新行创建一个新列表,因此它的大小始终为1。
// ...
Scanner file = new Scanner(new File(filename));
List<String> lines = new ArrayList<String>(); // <- declare lines as List
while(file.hasNextLine()) {
// ...
BTW - I wouldn't name the char[][]
variable map
. A Map is a totally different data structure. This is an array, and if you create in inside the loop, then you may encounter the same problems like you have with the list. But now you should know a quick fix ;)
BTW - 我不会命名char [] []变量映射。 Map是一种完全不同的数据结构。这是一个数组,如果你在循环内创建,那么你可能会遇到与列表一样的问题。但现在你应该知道一个快速修复;)
#2
0
Change the code as following:
更改代码如下:
public static void main(String[] args) {
char[][] map = null;
try {
Scanner file = new Scanner(new File("textfile.txt"));
ArrayList<String> lines = new ArrayList<String>();
while (file.hasNextLine()) {
String line = file.nextLine();
lines.add(line);
}
map = new char[lines.size()][];
} catch (IOException e) {
System.out.println("IOException");
}
System.out.println(map.length);
}