Substring indexes off in simple map reading program

Alright, so I’m trying to create a simple RPG game, and my map reading code seems to be off.
This is the map reading line:


    Integer.parseInt(map.data.substring(Map.MAP_START + ((playerPOS - map.width)/2) - 1, Map.MAP_START + ((((playerPOS - map.width)/2) - 1) + map.dataLen)));

Now the only tiles in the map are 01 and 00, so when I see a 10 I know something is wrong:


    (playerPOS - map.width) = 34 playerPOS = 50 player.x = 2 player.y = 3 blah = 0
    (playerPOS - map.width) = 18 playerPOS = 34 player.x = 2 player.y = 2 blah = 10

Here is the map reading code:


    public void init(GameContainer gc) throws SlickException {
    		map = new Map();
    		player = new Player();
    		
    		keys = new boolean[ALL_KEYS];
    		for(int i = 0; i < ALL_KEYS; i++){
    			keys[i] = false;
    		}
    		
    		file = new File("testmap.txt");
    		
    		try {
    			fin = new FileInputStream(file);
    
    			bin = new BufferedInputStream(fin);
    			StringBuilder sb = new StringBuilder();
    			int ch = 0;
    			
    			while ((ch=bin.read())!=-1) {
    				sb.append((char)ch);
    			}
    			
    			map.data = sb.toString().replace(" ", "").replace("\n", "").replace("\r", "");
    			System.out.print(map.data);
    		
    			map.width = Integer.parseInt(map.data.substring(map.dataOffs, map.dataOffs + map.dataLen));
    			map.dataOffs += map.dataLen;
    			
    			map.height = Integer.parseInt(map.data.substring(map.dataOffs, map.dataOffs + map.dataLen));
    			map.dataOffs += map.dataLen;
    		}
    		catch (Exception e) {
    			e.printStackTrace();
    		}
    		
    		tiles = new Image("tiles.png");
    		hero = new Image("hero.png");
    	}

and here is the map file:


    16 12
    01 01 01 01 01 01 01 01 01 01 00 00 00 00 00 00
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    00 00 00 00 01 01 00 00 00 00 00 00 00 00 00 00
    00 00 00 00 01 01 00 00 00 00 00 00 00 00 00 00
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    01 00 00 00 00 00 00 00 00 01 00 00 00 00 00 00
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

So if anymore info is needed let me know.

Maybe something like this:


BufferedReader r = new BufferedReader(new FileReader(file));

String line = r.readLine();
String[] tokens = line.split("\\s+");
int cols = Integer.parseInt(tokens[0]);
int rows = Integer.parseInt(tokens[1]);
int[][] mapData = new mapData[rows][cols];

for (int i=0; i<rows; i++) {
   line = r.readLine();
   tokens = line.split("\\s+");
   for (int j=0; j<cols; j++) {
      mapData[i][j] = Integer.parseInt(tokens[j]);
   }
}

r.close();

Ahh, I didn’t think of doing it like that. Thank you very much :slight_smile: