I want to take text file data, and put it into a 2d int[][] array. I’ve tried several things and all has failed. How can I do this effectively and properly?
Heres my code:
public void readMap() throws IOException
{
try
{
in = new BufferedReader(new FileReader("testfile.txt"));
String currLine;
while((currLine = in.readLine()) != null)
{
System.out.println(currLine);
for(int x = 0; x < tile_nums; x++)
{
for(int y = 0; y < tile_nums; y++)
{
}
}
}
}
finally
{
in.close();
}
}
and my text file data:
100000000
111111010
100001010
111111110
000001000
000000000
000000000
000000000
000000000
000000000
Couple things,
First, in order to determine the size of the outter array you will need to determine the number of lines in the file. Or you can process each line to produce the inner arrays, store them in a List and create the outter array from the list.
Second, you need to take each line, determine the length (
line.length()) to create the inner array of the appropriate size (int[] inner = new int[line.length()];)Finally, convert the line to an array of
charsand convert eachcharto anintusingInteger.parseIntI am not posting the entire solution since I think this is probably homework.