Reputation: 53
I am making a top-down rpg game and I plan to use txt files and 2D arrays for levels, I am going to have a 2D array containing values from 0 to 6, each representing a different texture, I plan to keep these levels in a txt file, but I am having trouble reading in the file into a 2D array.
This is what my file looks like
0000000010000000044444444
0000050010000005504444444
0050000010000000005544444
0005000010000000000550550
0000000011111000333333333
0000000000001111100000000
0005000055000010000000000
0000000000000010000000000
0000050000500010000000000
0000000000000010000000000
0000000050005010000000200
0001111111111112200002000
0501000000000002200002000
0051000000000000000002000
This is my code for reading in my array, by the way its 14x25.
public int[][] readInLevel(String levelNumber)
{
try
{
BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(levelNumber)));
while (in.ready() == true)
{
for (int row = 0; row <= 13; row++)
{
for (int column = 0; column <= 24; column++)
{
level_1[row][column] = in.read();
}
}
}
} catch (Exception e)
{
System.out.println(e);
}
return null;
}
And this is my 2D array.
int level_1[][] = new int[14][25];
Upvotes: 0
Views: 1754
Reputation: 1846
Ok.
Circuit Smasher is correct in that you should subtract 48 the ascii value to get the int that corresponds to the character in your level file. You're seeing -49 because -1 (read()'s EOF return value) minus 48 is -49.
The restrictions in your for
loops are confusing to read; perhaps you should consider changing them to
for (int row = 0; row < 14; row++)
Then, remember that the 15th character of each row is the newline character. This is offsetting the grid. We can get rid of this newline character by reading it. (You could also just get the first 14 characters from each row of input using the other definition of BufferedReader.read()
Or you could use the Scanner
class which has a function to get an entire line of input.)
The following snippet appears to function correctly (I've added some print statements to ensure validity):
int[][] level_1 = new int[14][25];
try {
BufferedReader in = new BufferedReader(new InputStreamReader(
new FileInputStream("input.txt")));
while (in.ready() == true) {
for (int row = 0; row < 14; row++) {
for (int column = 0; column < 25; column++) {
level_1[row][column] = in.read() - 48;
System.out.print(level_1[row][column] + ", ");
}
in.read(); // this is important
System.out.println("");
}
}
} catch (Exception e) {
e.printStackTrace();
}
Additional edit: Are you using an IDE? In Eclipse, the input files need to be in the root directory of the project you're working on. It may be different for other IDEs. If you're still receiving a bunch of -49s then I'd think the file isn't being read properly.
Upvotes: 0
Reputation:
in.read
reads a an ASCII byte. Notice here that "0" in Asciii is 48. So, subtract 48 from in.read, like this: level_1[row][column]=in.read()-48;
If that doesn't work, then cast to int: level_1[row][column]=(int)(in.read-48);
EDIT: Okay, there's one way you can do it which is what I do. EDIT EDIT: checking for newlines
byte b=0;
int row=0;
int column=0;
while((b=in.read())!=1) {
if((char)b=='\n') {
row++;
column=0;
} else {
level_1[row][column]=b-48;
column++;
}
}
There might be some issues with the row/column code, I didn't think through it thoroughly.
EDIT EDIT EDIT: okay, found the problem. BufferedReader.read() returns -1 if there is no byte, meaning that it returns -49 each time because it return -1-48. That means that your reader isn't properly initialized. Check your file. I usually do:
File f=new File(path);
FileReader fr=new FileReader(f);
BufferedReader reader=new BufferedReader(fr);
Okay, here's my code: http://pastebin.com/qstGnK77
It works just fine, look at the output.
Upvotes: 1