Hi, I've written some code to do this, but it's not working as I expect and am at a loss on how to fix it.
This is what the ASCII map looks like:
################### #.................# #......G........E.# #.................# #..E..............# #..........G......# #.................# #.................# ###################
Here's how I've tried to store the information in a 2D array:
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.Arrays; public class Map { public void importMap() throws IOException{ BufferedReader br = new BufferedReader(new FileReader("map.txt")); String line; while ((line = br.readLine()) != null){ for (char ch: line.toCharArray()){ final int M = 19; final int N = 9; char [][] matrix = new char[M][N]; for (int r = 0; r < M; r++){ for (int c = 0; c < N; c++){ matrix[r][c] = ch; } System.out.println(Arrays.deepToString(matrix)); } br.close(); } } } public static void main(String[] args) throws IOException{ Map r = new Map(); r.importMap(); } }
But it's not printing out what I'm expecting. It's printing out:
[[#, #, #, #, #, #, #, #, #], [#, #, #, #, #, #, #, #, #], [#, #, #, #, #, #, #, #, #], [#, #, #, #, #, #, #, #, #], [#, #, #, #, #, #, #, #, #], [#, #, #, #, #, #, #, #, #], [#, #, #, #, #, #, #, #, #], [#, #, #, #, #, #, #, #, #], [
What is printed is much larger than what I've copy and pasted but I can't post it all here.
The error I get is:
Exception in thread "main" java.io.IOException: Stream closed at java.io.BufferedReader.ensureOpen(Unknown Source) at java.io.BufferedReader.readLine(Unknown Source) at java.io.BufferedReader.readLine(Unknown Source) at Map.importMap(Map.java:26) at Map.main(Map.java:44)
Thanks for the help!