I have a Class that needs to be return a Object built from a String and im not sure whats best to use for my situation.
my string comes in as JSONString and i am splittig it to get key value pairs that look like this.
variable = "x"
value = 60%
public class Math {
public String x = "";
public String y = "";
Solution 1: this is working but im not able to cast it into class.
String variable = keyVal[0];
String value = keyVal[1];
Map m = new HashMap();
m.put(variable, value);
Solution 2: I need to reference to Math.x via the variables
value but thats not working
String variable = keyVal[0];
String value = keyVal[1];
Math c = new Math();
c.variable = value;
CodePudding user response:
String variable = keyVal[0];
String value = keyVal[1];
Math c = new Math();
c.variable = value;
I think you mean:
String variable = keyVal[0];
String value = keyVal[1];
Math c = new Math();
if(variable.equals("x") {
c.x = value
}
if(variable.equals("y") {
c.y = value
}
You need to do some kind of case distinction, it's not possible to take a String and use it to dynamically determine which field to write to (ok with Reflection it is possible but that would be even more complicated).