0

I tried to loop through a string array such that for each value, it calls dynamically the setter method named with it, e.g one.set"holdingArray[i]"(a);. Is there a way to achieve this behavior? Here is a code example to illustrate my problem. The line one.setholdingArray[i](a); is compiling and must be changed.

class Troops {
    private int barbarian;
    private int archer;
    private int goblin;
    private int giant;

    private String[] holdingArray =  {
            "Barbarian",
            "Archer",
            "Goblin",
            "Giant",
    };

    int getBarbarian() {
        return barbarian;
    }
    int getArcher() {
        return archer;
    }
    int getGoblin() {
        return goblin;
    }
    int getGiant() {
        return giant;
    }

    void setBarbarian(int barb) {
        barbarian = barb * 150;
    }
    void setArcher(int a) {
        archer = a * 300;
    }
    void setGoblin(int g) {
        goblin = g * 80;
    }
    void setGiant(int gi) {
        giant = gi * 2250;
    }

    class HelloWorld {
        public static void main(String args[]) {
            Scanner in = new Scanner(System.in);
            Troops one = new Troops();
            int a;

            for(int i = 0; i < holdingArray.length; i++) {
                System.out.println("How many " + holdingArray[i] + " do you have??");
                a = in.nextInt();
                // TODO this line must be changed with the answer
                one.setholdingArray[i](a);
                System.out.println();
            }
        }
    }
3
  • what language is that? Commented Jan 5, 2016 at 19:07
  • 1. Try tagging what language you're using (looks like Java to me). 2. To assign to an array, do something like myArray[i] = x 3. Your array is private, so it can't be modified or even accessed by code from outside of the class. Commented Jan 5, 2016 at 19:08
  • one.setholdingArray[i](a); This line makes no sense. First of all, Class Troop has no method setHoldingArray, and second, you then try to do an array access on it, which isn't valid syntax. Commented Jan 5, 2016 at 19:19

2 Answers 2

1

It looks like you're trying to implement a Map - https://docs.oracle.com/javase/7/docs/api/java/util/Map.html

Your keys are the troop types (Barb, Archer, etc.) and their values are their counts. Looks at a glance to be

Map<String, Int>.

If you want to do it your way then you could just call your different setters depending on the i value...like if i == 0 then setBarbarian, i == 1 setArcher. Using a map would be much more efficient.

Sign up to request clarification or add additional context in comments.

Comments

0

I think this is what you want,

public static void main(String args[]) {

    Scanner in = new Scanner(System.in);
    Troops one = new Troops();
    int a;
    Method method = null;
    for (int i = 0; i < one.holdingArray.length; i++) {

        System.out.println("How many " + one.holdingArray[i] + " do you have??");
        a = in.nextInt();

        try {
            method = one.getClass().getDeclaredMethod("set" + one.holdingArray[i], Integer.class);
            method.invoke(one, a);

        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    System.out.printf("");

}

static class Troops {

    private int barbarian;
    private int archer;
    private int goblin;
    private int giant;

    public String[] holdingArray = {
            "Barbarian",
            "Archer",
            "Goblin",
            "Giant",
    };


    //Getters.
    int getBarbarian() {
        return barbarian;
    }

    int getArcher() {
        return archer;
    }

    int getGoblin() {
        return goblin;
    }

    int getGiant() {
        return giant;
    }


    //Setters.

    void setBarbarian(Integer barb) {
        barbarian = barb * 150;
    }

    void setArcher(Integer a) {
        archer = a * 300;
    }

    void setGoblin(Integer g) {
        goblin = g * 80;
    }

    void setGiant(Integer gi) {
        giant = gi * 2250;
    }
}

}

1 Comment

Change method = Troops.class.getMethod("get" + one.holdingArray[i]); to method = Troops.class.getMethod("set" + one.holdingArray[i]);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.