# [java] String to variable question

## Recommended Posts

I was looking on how to make a string into a variable.
string input = "deh.gmzs"

How can I make it do this.
System.out.println(deh.gmzs)

Note: deh.gmzs represents another variable of any type. In other words I want the user to input the name of another variable in the program and then I want the program to output the value of the variable.

##### Share on other sites
There is no direct way of doing that.

You can hardcode it though, something like...

if(str=="variable_name_1")
output(variable_name_1)
if(str=="variable_name_2")
output(variable_name_2)

...And so on. This is just a logic example, if you want to code this you might want to use an hash table instead.

##### Share on other sites
Reflection might solve part of the problem.

##### Share on other sites
Quote:
 Original post by TallshortkidI was looking on how to make a string into a variable.string input = "deh.gmzs"How can I make it do this.System.out.println(deh.gmzs)Note: deh.gmzs represents another variable of any type.In other words I want the user to input the name of another variable in the program and then I want the program to output the value of the variable.

As posted above, reflection is indeed the answer.
I presume this is for a console type system where you can get and set variables on the fly or something. First off you'll probably want to parse the string , you'll (hopefully) be dealing with fairly simple constructs like the above, so StringTokenizer is a fairly simple solution to this.

If by the above you mean an objectInstance.variableValue then you need to find the classtype of 'deh' first. This is implementation dependent , my example presumes that you have some sort of collection of objects and you can get back a particular object by name (ie "deh") using some query. On any object you can use 'objectInstance.getClass()' to get the Class object of the object.

The Class object has a number of methods that you can use to query for methods and fields in the class definition. theres a .getField(String name) which will do the job for the above (ie getField("gmzs")).

Having got the field you can do a couple of things. If as above, all you want to do is print out the value you can just do a toString() on it, if you want to be more inventive theres a host of methods in the Class and Field classes to query types and to get and set values.

Obviously theres a whole pile of checks and exceptions I haven't mentioned that you'll have to consider :) Hope this is some help.If you're a little more specific about what exactly you want to accomplish then I can give more info.

If you're using 1.5 then heres an interesting article about even more RTTI that you can include in your code:

http://www.onjava.com/pub/a/onjava/2004/04/21/declarative.html

D.

##### Share on other sites
I'm actually using it to make a built in debugger where it will open a GUI and have me input the variable and then output the value.

Thank you very much for that post, it was very helpful.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628372
• Total Posts
2982305

• 10
• 9
• 13
• 24
• 11