I'm trying to set variables from a text file. If the string I get is exactly the same name as a variable in my class, can I set it directly? For example:

int myValue;

String s = "myValue";
int i = Integer.parseInt("42");

Is there a better way to assign the value besides doing:

if (s.equals("myValue"))
myValue = i;

06-29-2011, 05:46 PM

doWhile

Quote:

Is there a better way to assign the value besides doing:

The best way is the way that works. Does this work for what you are trying to accomplish? There are quite possibly many different way to accomplish what you are doing, the 'best' way is quite relative to what you are trying to accomplish in the grand scheme of things.