user4587101
user4587101

Reputation:

How can a string be used to call an object method in Java?

I have a class called "Skill" with a getName() method. I use it to return the name of the Skill objects that I create. When I try to compile my code, it says "Cannot find symbol- method getName()". Is there any way to get around this? For example, in my main method:

String[] playerSkill = new String[1];
playerSkill[0] = "a";
System.out.println(playerSkill[0].getName());

And in my Skill class:

public Skill(String n, String d, int p) {
    name = n;
    description = d;
    power = p;
}

public String getName() {
    return name;
}

I know I cannot make the String look like a Skill object:

System.out.println((Skill)playerSkill[0].getName());

The main problem is that the arrays are to be filled with user input so the arrays need to be String arrays. How would I get around this?

Upvotes: 0

Views: 112

Answers (4)

shikjohari
shikjohari

Reputation: 2288

When you are saying String[] playerSkill = new String[1]; That means that the array created is of String type and you ll only be able to call methods of String class. The element at playerSkill[0] can only hold object of String and not Skill

I think you want to achieve this

    Skill[] playerSkill = new Skill[1];
    playerSkill[0] = new Skill("a", "b", 0);
    System.out.println(playerSkill[0].getName());

Since you mentioned you need to take input and then make the skill object. You can get the string, int from the console and then create the instances of Skill

Scanner sc = new Scanner(System.in);
String name = sc.next();
String desc = sc.next();
int x = sc.nextInt();
Skill s = new Skill(name, desc, x);

you need to modify these lines according to your stuff

Upvotes: 1

Sezin Karli
Sezin Karli

Reputation: 2525

If you need such a behavior I suggest to create a POJO such as:

public class Player{
    private String skill, name;
    public setSkill(String skill){
        this.skill = skill;
    }
    public getSkill(){ return this.skill;}
    public setName(String name){
        this.name=name;
    }
    public getName(){return this.name;}
}

Upvotes: 0

Makoto
Makoto

Reputation: 106430

Since the String object doesn't have a getName method attached to it, Java isn't going to allow you to call it on an instance of it.

Use the object that has the method attached to it. Instead of an array though (since you only create one space in it), just go with a regular instance.

Skill skill = new Skill("name", "desc", 10);
System.out.println(skill.getName());

This is important to note because you may run into situations in which other objects will have similarly named methods, but not give you the behavior you want. Always be specific about the type of object you're interacting with.

Upvotes: 0

mlethys
mlethys

Reputation: 436

Your method getName is from class Skill and you try to invoke it from String class. You should do something like this:

Skill[] playerSkill = new Skill[1];
playerSkill[0] = new Skill("name", "desc", 3);
System.out.println(playerSkill[0].getName());

Upvotes: 5

Related Questions