Rexy92
Rexy92

Reputation: 1

Use an array string element to call a method dynamically?

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();
            }
        }
    }

Upvotes: 0

Views: 49

Answers (2)

Msp
Msp

Reputation: 2493

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;
    }
}

}

Upvotes: 0

EDEdDNEdDYFaN
EDEdDNEdDYFaN

Reputation: 21

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.

Upvotes: 1

Related Questions