0

I have 201 text files in my raw folder naming level0.txt,level1.txt,level2.txt........

I want to read them but dont wanna get in trouble of writing 201 lines of code

so far i have tried this

    private void readLevelName() {
          for (int i = 0 ; i <= 200 ; i++){
              String s = RRaw.substring(0,11)+i;
               map[i]= Integer.parseInt(s);
    // Log.e(""+i , s);
  }

RRaw is my string containing "R.raw.level", map[] is an int array

Error is

 Caused by: java.lang.NumberFormatException: Invalid int: "R.raw.level0"

guys any help will be appreciated

Shubham
  • 72
  • 1
  • 10

1 Answers1

0

You can't parse a string like "R.raw.level0" to a int.

If you want to get the raw data by name you should use getIdentifier().

Your code should become something like this:

private void readLevelName() {

   String packageName = getPackageName();

   for (int i = 0 ; i <= 200 ; i++){
              String s = RRaw.substring(0,11)+i;
              map[i]= getResources().getIdentifier(s, "raw", packageName);
   }
}
P1x
  • 1,690
  • 1
  • 19
  • 24