Search code examples
javaandroidinputstreamreader

Problems using Android Scanner+BufferedReader


I've been working on adapting code from an existing java project into the android environment.

I load a map from a text file of integers which correspond to a sprite sheet.

e.g. 0=grass 1=wall

0 0 0 1 1 1 0 0 0
0 0 0 1 1 1 0 0 0
0 0 0 1 1 1 0 0 0

-

this was previously done with this: (A scanner and buffered reader)

 mapData = new Scanner(new BufferedReader(new FileReader(fileName)));
  mapData.next();
  for (int i = 0; i < 3; i++) {
    for (int y = 0; y < 30; y++) {
      for (int x = 0; x < 50; x++) {
        map[i][x][y] = mapData.nextInt();
      }
    }
    mapData.next();
  }

-

I have been struggling to adapt this the past couple days, its in a state where its working but its taking 5 minutes to do a 1 second job

            mapData = new Scanner(new BufferedReader(new InputStreamReader(context.getResources().openRawResource(map1))));
        mapData.next();
        for (int i = 0; i < 3; i++) {
            for (int y = 0; y < 30; y++) {
                for (int x = 0; x < 50; x++) {
                    map[i][x][y] = mapData.nextInt();
                }
            }
            mapData.next();
        }

(map1 being a .txt file passed on by another class)

What am I doing wrong here or what other methods should I use to accomplish this task?

Thanks -Lyndon


Solution

  • It seems I have found a sort of fix to the issue.

    Replacing

    map[i][x][y] = mapData.nextInt();
    

    With:

    map[i][x][y] = Integer.parseInt(mapData.next());
    

    Made huge fixes to the speed/