-1

So I've scoured the internet for a fix, and i've gotten tips to use maps, the reflect method, and a ton of other stuff, but nothing has worked the way I wanted.

My goal is to do something like this:

I have a string divided into an array. Example: "setVal strength 3"

lineArray[0] = setVal

lineArray[1] = strength

lineArray[2] = 3

I want to take lineArray[1] and add "Feats." to the beginning of it, so it it, for example, something like "Feats.strength" (which I can do with a string variable)

I then want to set that variable (Feats.strength, it's a double called strength in the Feats class) to lineArray[2] (which is a double).

else if(lineArray[0].equals("setVal") && lineArray.length == 2){

    //Take lineArray[1], which is the name of a variable in 
    another class, specifically Feats.strength, Feats.agility, etc.
    //Set that value in lineArray[1] to lineArray[2]

    //Something like
    set("Feats." + lineArray[1], lineArray[2]);

    Feats.resetStat();

}

Does that make any sense? Thanks for the help in advance!

Jacob B.
  • 423
  • 3
  • 12

2 Answers2

0

Maybe something like this would work?

// Application.java
public class Application {

    public static void main(String[] args){
        String field = "setVal strength 3";
        Feats.resolveValue(field);

        String field2 = "getVal strength";
        Integer value = Feats.resolveValue(field2);

        String field3 = "clearAll";
        Feats.resolveValue(field3);
    }
}

--

// Feats.java
public class Feats {

    private static final Logger LOGGER = LoggerFactory.getLogger(Feats.class);
    private static final Map<String, Integer> ATTRIBUTES = new HashMap<>();

    public static String resolveString(String fieldInput){

         String response = null;
         String[] values = fieldInput.split(" ");
         Action action; 

         try {
             action = Action.valueOf(values[0].trim().toUppercase());

         } catch(Exception e){
             LOGGER.error("Not a valid Action in input: " + fieldInput);
             return response;
         }
         switch(action){
             case SETVALUE:
                setValue(values[1].trim(), values[2].trim());
                break;

             case CLEARALL:
                clearAll();
                break;

             case GETVALUE:
                response = getValue(values[1].trim());
                break;

             default:
                assert true;
         }

         return response;
    }

    private static void setValue(String attrName, String attrValue){
        ATTRIBUTES.put(attrName, attrValue);
    }

    private static String getValue(String attrName){
         return ATTRIBUTES.get(attrName);
    }

    private static void clearAll(){
        ATTRIBUTES.clear();
    }
}

--

// Action.java
public enum Action {
    SETVALUE, CLEARALL, GETVALUE
}
wheeleruniverse
  • 1,511
  • 2
  • 20
  • 37
0

So I solved it. Thanks to @shmosel for telling me to use a field.

long temp = Long.parseLong(lineArray[2]);

try {
Feats.class.getField(lineArray[1]).set(lineArray[1], temp);;
} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e1) {
e1.printStackTrace();
}
Jacob B.
  • 423
  • 3
  • 12