0

Hi I have a strange question about java. I will leave out the background info so as not to complicate it. If you have a variable named fname. And say you have a function returning a String that is "fname". Is there a way to say reference the identifier fname via the String "fname". The idea would be something like "fname".toIdentifier() = value but obviously toIdentifier isn't a real method.

I suppose a bit of background mite help. Basically I have a string "fname" mapped to another string "the value of fname". And I want a way to quickly say the variable fname = the value of the key "fname" from the map. I'm getting the key value pair from iterating over a map of cookies in the form . And I don't want to do "if key = "fname" set fname to "value of fname" because I have a ton of variables that need to be set that way. I'd rather do something like currentkey.toIdentifer = thevalue. Weird question maybe I'm overlooking a much easier way to approach this.

jahroy
  • 22,322
  • 9
  • 59
  • 108
Yoshi
  • 115
  • 11
  • [yes is possible](http://stackoverflow.com/questions/6629995/test-if-a-class-contains-an-instance-variable-based-on-its-name) , but its for testing, there are a few good ways, this question talking about wrong desing – mKorbel Oct 11 '12 at 06:30
  • It seems like you just want a Map: http://docs.oracle.com/javase/6/docs/api/java/util/Map.html _OR_ you might want to look into reflection.... _OR_ you might want to think about your design a bit. – jahroy Oct 11 '12 at 06:43
  • I agree the design is probably flawed but I had never thought about doing something like that. – Yoshi Oct 11 '12 at 06:55
  • I'm pretty certain I don't understand the question. A little more explanation would help... – jahroy Oct 11 '12 at 06:57
  • @jahoy...ya thats why originally I was going to leave out the details. The question is essentially, given a string, can I set a variable whose identifier is equal to that string by using the actual String object. So if I had String name = "bob"; could I do something like "name" = "fred" and have that be equivalent to name = "fred" through some type of parsing/conversion or any other way. – Yoshi Oct 11 '12 at 07:03
  • edited my answer... but it's still hard to follow what you're saying. – jahroy Oct 11 '12 at 08:42

2 Answers2

2

Why don't you just use a simple hashmap for this?

Map<String, String> mapping = new HashMap<String, String>();
mapping.put("fname", "someValue");
...

String value = mapping.get(key); //key could be "fname"
flash
  • 6,730
  • 7
  • 46
  • 70
  • I appreciate your answer. I am using a hashmap at the moment. I think I should be using contains() and then setting fname rather than iterating over the whole map since I know the cookie's I'm expecting. But yes I will accept this. THanks for the swift reply! I think I am preparing for have many many cookies when in reality that is probably quite uncommon. A better idea would be one cookie and pull the rest of the info from a database based on a username cookie. – Yoshi Oct 11 '12 at 06:57
  • Map has a method named _containsKey_: http://docs.oracle.com/javase/6/docs/api/java/util/Map.html#containsKey%28java.lang.Object%29 – jahroy Oct 11 '12 at 06:59
1

In a way you're describing what reflection is used for:

You refer to an object's fields and methods by name.

However, most of the time when people ask a question like this, they're better off solving their problem by re-working their design and taking advantage of data structures like Maps.


Here's some code that shows how to create a Map from two arrays:

String[] keyArray = { "one", "two", "three" };
String[] valArray = { "foo", "bar", "bazzz" };

// create a new HashMap that maps Strings to Strings

Map<String, String> exampleMap = new HashMap<String, String>();

// create a map from the two arrays above

for (int i = 0; i < keyArray.length; i++) {
    String theKey = keyArray[i];
    String theVal = valArray[i];
    exampleMap.put(theKey, theVal);
}

// print the contents of our new map

for (String loopKey : exampleMap.keySet()) {
    String loopVal = exampleMap.get(loopKey);
    System.out.println(loopKey + ": " + loopVal);
}

Here's a link to the JavaDoc for Map.

jahroy
  • 22,322
  • 9
  • 59
  • 108