Populating 2D array from text file using scanner Java -
i trying populate 2d array in java through reading text file using scanner. currently, don't understand what's going wrong. below text file i'm using, 2d array when printed , code.
text file
30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 28 22 23 29 30 30 30 30 30 30 30 30 30 30 30 30 30 30 28 22 33 9 10 34 23 29 30 30 30 30 30 30 30 30 30 30 28 22 33 9 17 1 1 18 10 34 23 29 30 30 30 30 30 30 28 22 33 9 17 1 1 1 1 1 1 18 10 34 23 29 30 30 28 22 33 9 17 1 1 1 1 1 1 1 1 1 1 18 10 34 23 29 33 9 17 1 1 2 1 1 1 1 1 1 1 1 6 1 1 18 10 34 32 8 16 1 1 1 1 1 1 1 1 1 1 1 1 5 1 15 7 31 27 25 32 8 16 1 1 3 1 1 1 1 1 1 1 15 7 31 24 26 30 30 27 25 32 8 16 1 1 1 1 1 1 15 7 31 24 26 30 30 30 30 30 30 27 25 32 8 16 1 1 15 7 31 24 26 30 30 30 30 30 30 30 30 30 30 27 25 32 8 7 31 24 26 30 30 30 30 30 30 30 30 30 30 30 30 30 30 27 25 24 26 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30
printed array
map array: [[30, 30, 30, 30, 1, 1, 1, 32, 30, 30, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0], [30, 30, 30, 30, 10, 1, 1, 16, 30, 30, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0], [30, 30, 30, 30, 23, 6, 1, 1, 27, 30, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0], [30, 30, 30, 30, 30, 1, 1, 7, 24, 30, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0], [30, 30, 30, 30, 28, 10, 7, 24, 30, 30, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0], [30, 30, 30, 28, 33, 32, 24, 30, 30, 30, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0], [30, 30, 28, 33, 17, 16, 30, 30, 30, 30, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0], [30, 30, 23, 17, 1, 1, 27, 30, 30, 30, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0], [30, 30, 30, 1, 1, 1, 32, 30, 30, 30, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0], [30, 30, 30, 10, 1, 1, 16, 30, 30, 30, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0], [30, 30, 30, 23, 1, 1, 1, 27, 30, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [30, 30, 30, 30, 1, 1, 1, 32, 30, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [30, 30, 30, 30, 10, 1, 1, 7, 30, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [30, 30, 30, 30, 23, 1, 7, 24, 30, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [30, 30, 30, 28, 33, 7, 24, 30, 30, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [30, 30, 28, 33, 17, 27, 30, 30, 30, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [30, 30, 33, 17, 1, 32, 30, 30, 30, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [30, 30, 10, 1, 1, 16, 30, 30, 30, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [30, 30, 23, 1, 1, 1, 27, 30, 30, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
currently, code reads every integer in while loop. whilst x-coordinate (the x position in array) increases 19 in array, set 0 , y-coordinate increases (the y position in array). method used populate array.
note: width , height of 2d array (mapwidth , mapheight) , text file both 20 * 20. additionally, initial value of xcoord , ycoord 0.
my code
while (filescannertilemap.hasnext()) { if (filescannertilemap.hasnextint()) { tilemap[xcoord][ycoord] = filescannertilemap.nextint(); } filescannertilemap.next(); // increase xcoord each time xcoord++; // when xcoord reaches max @ 19, increase ycoord , set xcoord equal 0 if (mapwidth - 1 == xcoord) { ycoord++; xcoord = 0; } }
any appreciated!
edit
i want create 2d array matches text file can later reference values of 2d array in sprite sheet create terrain 2d game.
so text file --> 2d array --> terrain generation
you missing integers because next
twice: filescannertilemap.nextint();
, filescannertilemap.next();
try that:
if (filescannertilemap.hasnextint()) { tilemap[xcoord][ycoord] = filescannertilemap.nextint(); } else { filescannertilemap.next(); }
and break loop when done:
if (mapwidth - 1 == xcoord) { ycoord++; xcoord = 0; // add this: if (mapwidth - 1 == ycoord) { break; } }
Comments
Post a Comment