0

Is there a way to get a Integer variable from a String object, something like:

String string = "Render.SCREEN_WIDTH_TILES";
// SCREEN_WIDTH_TILES is a Render Integer value referenced in other class

I want the string variable to hold the reference to the specified int.

What I want from that string value is to "transform it" into a int value,

Is it possible to do this?

I can't find a way to handle an integer value as a variable in a string.

Jonathan Lam
  • 15,294
  • 14
  • 60
  • 85
fer
  • 11
  • 3

1 Answers1

3

You seem to be misunderstanding how Integer.parseInt(s) works. The documentation clearly states:

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 parameter to Integer.parseInt(s) must be a string that contains a number. Such as:

  • Integer.parseInt("12345")
  • Integer.parseInt("-45")

But not:

  • Integer.parseInt("Hello world")
  • Integer.parseInt("this will not work 4 you")

And certainly not: Integer.parseInt("Render.SCREEN_WIDTH_TILES - 1");

sstan
  • 32,273
  • 5
  • 41
  • 62
  • thank for you answer, any solution to my problem i doubt using parseInt could be the solution, what i need is to convert a string into a int variable is it possible? – fer Sep 26 '15 at 02:31
  • The problem is that it really isn't clear to me what you are trying to do, so I don't see how I can recommend anything useful. All I know for sure is that `Integer.parseInt(s)` is not being used correctly here. – sstan Sep 26 '15 at 02:33
  • I update my question hope it's clear now thank you – fer Sep 26 '15 at 02:42