-5

I have code like this :

int [] arrayOfImages = new int[namesOfSubjectsColorCode.size()];
    int y = 0;    
        for (int x = 0 ; x<namesOfSubjectsColorCode.size();x++) {
            nameOfColorCode = namesOfSubjectsColorCode.get(x);
            String str = "com" + "." + "nyurals" + "." + "R" + "." + "drawable" + "." + nameOfColorCode;
            arrayOfImages[y] = Integer.parseInt(str);
            // Integer.parseInt(str);
            y++;
        }

Here, I have created integer array. Then, I have created string and by using Integer.parseInt() I want to convert it to int so that, my array of integer should generate dynamically. It is giving NumberFormatException.

Please suggest to me a solution for this.

Bosko Mijin
  • 3,287
  • 3
  • 32
  • 45
  • Because "com.nyurals.R.drawable.." is not an Integer. – Ken Wolf Nov 29 '13 at 13:57
  • You're expecting it to look up that resource... that's not going to happen. – Jon Skeet Nov 29 '13 at 13:57
  • can you suggest any solution.. beacause, I have color code for 10 subjects coming from Webservices and I have to map this to R.drawable image of resource folder.. please suggest dear friend... – user2890202 Nov 29 '13 at 14:00
  • use eval() function http://stackoverflow.com/questions/8666935/implemention-of-eval-in-java – Dev Nov 29 '13 at 14:02
  • Maybe you need to check this: http://stackoverflow.com/questions/3476430/how-to-get-a-resource-id-with-a-known-ressource-name – Boban S. Nov 29 '13 at 14:02
  • use eval() function http://stackoverflow.com/questions/8666935/implemention-of-eval-in-java – Dev Nov 29 '13 at 14:03
  • look at this http://stackoverflow.com/questions/4427608/android-getting-resource-id-from-string – grexter89 Nov 29 '13 at 14:03

7 Answers7

1

Argument for Integer.parseInt() is invalid, you can't pass it string like "com.nyurals.." etc

From the docs:

public static int parseInt(String s)
                throws NumberFormatException

Parses the string argument as a signed decimal integer. The characters in the string must all be decimal digits, except that the first character may be an ASCII minus sign '-' ('\u002D') to indicate a negative value or an ASCII plus sign '+' ('\u002B') to indicate a positive value. The resulting integer value is returned, exactly as if the argument and the radix 10 were given as arguments to the parseInt(java.lang.String, int) method.

And that's exactly what you're getting: NumberFormatException.

EDIT:

You probably want to do something like this:

nameOfColorCode = namesOfSubjectsColorCode.get(x);
String str = "" + nameOfColorCode;

int resourceId = this.getResources().getIdentifier(str, "drawable", this.getPackageName());

arrayOfImages[y] = resourceId;
y++;
Melquiades
  • 8,496
  • 1
  • 31
  • 46
1

There is no way for this String to be reasonably turned into an int.

    String str = "com" + "." + "nyurals" + "." + "R" + "." + "drawable" + "." + nameOfColorCode;

Something like this would be expected:

String str = "1";
Kevin Bowersox
  • 93,289
  • 19
  • 159
  • 189
1

Its obvious that it gives you NumberFormatException. Look at your code :

Your str variable contains String which cant parse into Integer value.

Vimal Bera
  • 10,346
  • 4
  • 25
  • 47
0

Integer.parseInt() expects to be given an integer of some sort, not a string of the format com.nyurals.R.drawable.<<nameOfColorCode>>.

Generally, in Android, you would access the variables directly with something like:

R.drawable.SomeName

rather than trying to decode (or, in your case, directly using) the string to get a value.

For example, the following code gets the surface view based on an ID:

SurfaceView sv = (SurfaceView)findViewById(R.id.PfrRightAntiView);

If you do need to dynamically extract a resource based on a string known only at runtime, look into Resources.getIdentifier(), an example of which can be found here.

As per this article, that method may not be the fastest. It may be better to use the reflection method as shown in that link.

Community
  • 1
  • 1
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
0

If you want to get the int (id) of a resource than you should use

Resources res = activity.getResources();
res.getIdentifier("name","resourceType",activty.getPackageName());

change the name with actual name and resourceType with actual resource type (drawable,color,etc); As others wrote you don't have an int in your string, it's just an int and not a reference to resoruce

Eddy
  • 489
  • 4
  • 10
0

You can access the constants you're trying to reference via http://docs.oracle.com/javase/tutorial/reflect/ but you really don't want to do that.

You'd be better served putting the int's in some form of map of which you can then select some kind of subset dependent on changing information

Bart Enkelaar
  • 695
  • 9
  • 21
0

May your str returns null so Integer.parseInt(null) return number format exception

Piyush Srivastava
  • 357
  • 1
  • 4
  • 21