Sign in to follow this  
Tallshortkid

[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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by Tallshortkid
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.


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 this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this