我正在尝试将文本文件转换为二维字符数组。我是那里的一部分,但我的阵列的最后一行不完全正确。这是我的代码:
protected void readMap(String fileName) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(fileName));
char[] chars;
int lines=0;
while (br.readLine() != null) {
lines++;
}
File file = new File(fileName);
try (FileReader reader = new FileReader(file)) {
chars = new char[(int) file.length()];
reader.read(chars);
reader.close();
} catch (IOException e) {
}
int columns = ((int) file.length())/lines;
map = new char[lines][columns];
for(int i=0; i<lines;i++){
for(int j=0;j<columns;j++){
map[i][j] = chars[j%columns+i*columns];
}
}
for(int ro=0; ro<map.length; ro++){
for(int colum=0; colum<(map[0].length); colum++){
System.out.print(map[ro][colum]);
}
}
return null;
}
这是输出:
##########################
#........................#
#.....###........###.....#
#......G..........G......#
#........................#
#...........E............#
#......G.........G.......#
#........G.....G.........#
#..........###...........#
#........................#
#################
^missing #'s here
我很困惑为什么会发生这种情况。我已经尝试改变我打印数组的方式,但我很确定它如何处理我如何将1d'字符'数组转换为2d'map'数组。 我真的输了,所以任何帮助都会非常感激!谢谢。
答案 0 :(得分:0)
我刚刚执行了代码,地图按预期打印。问题可能在于文件本身,因为这是不常见的因素。
修改强> 您观察到的结果是因为您可能在文件的末尾或某个位置有新的行字符或其他特殊字符。删除它你应该看到你想要的一致地图。
<强>替代强>
protected static void readMap(String fileName) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(fileName));
String line = "";
List<String> lines = new ArrayList<>();
while ((line = br.readLine()) != null) {
lines.add(line);
}
char[][] chars = new char[lines.size()][];
for (int col = 0; col < lines.size(); col++) {
for (int row = 0; row < lines.get(col).length(); row++) {
chars[col] = lines.get(col).toCharArray();
}
}
for (int col = 0; col < chars.length; col++) {
for (int row = 0; row < chars[col].length; row++) {
System.out.print(chars[col][row]);
}
System.out.println();
}
}//My rows and cols may be back to front
其他几点说明:
char[] chars = null;
会在这种情况下执行此操作。答案 1 :(得分:0)
我猜你的文件看起来像这样
##########################
#........................#
#.....###........###.....#
#......G..........G......#
#........................#
#...........E............#
#......G.........G.......#
#........G.....G.........#
#..........###...........#
#........................#
##########################
如果您打印文件长度,您将看到文件长度为296
代码为row = 11
和columns = 26
当您复制到地图时,您要复制到11 * 26 = 286
尝试下面的更新代码
public void readMap(String fileName) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(fileName));
int lines = 0, columns = 0;
String str;
List<String> lineList = new ArrayList<>();
while ((str = br.readLine()) != null && str.length() != 0) {
lines++;
columns = Math.max(columns, str.length()); // as it's not fixed
lineList.add(str);
}
System.out.println("Row : " + lines);
System.out.println("Columns : " + columns);
char[][] map = new char[lines][columns];
for (int i = 0; i < lines; i++) {
String currentLine = lineList.get(i);
int idx = 0;
for (int j = 0; j < currentLine.length(); j++) {
map[i][j] = currentLine.charAt(idx++);
}
}
for (int r = 0; r < map.length; r++) {
for (int c = 0; c < (map[0].length); c++) {
System.out.print(map[r][c]);
}
System.out.println();
}
}