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.

1
  • No, you can not evaluate a String like this that's supposed to name another constant. Commented Sep 26, 2015 at 5:49

1 Answer 1

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");

Sign up to request clarification or add additional context in comments.

3 Comments

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?
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.
I update my question hope it's clear now thank you

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.