-1

I have an abstract class like this

public abstract class Command {
    public abstract void execute(String keyWord[]);
    String keyWord;
    public Command(String keyWord) {
        this.keyWord = keyWord;
    }
}

and a class to manage it like so:

public class CommandManager {

    private static List<Command> commands = new ArrayList<>();


    public static void append(Command command) {
        commands.add(command);
    }

    static {

    }

    public static void load() {
        append(new Command("lol") {

            @Override
            public void execute(Player player, String[] keyWord) {
                System.out.println("hi");
            }
        });
    }

    public boolean handle() {
        String cmd[] = input.split(" ");
        Command command = commands.get(cmd[0].toLowerCase()); //this
        if (command != null) {
            command.execute(player, cmd);
            return true;
        }
        return false;
    }

}

The error I get is where my comment is. How can I use the the get method to get the String from the Command class? Thanks

Greg E.
  • 2,722
  • 1
  • 16
  • 22
  • I take it this is Java? If so, you should tag it as such. – Blorgbeard Jun 22 '12 at 05:19
  • Give more information about what the error is. – Elliot Chance Jun 22 '12 at 05:21
  • you need to expose the string from the command class, say by using a getter, a method like String getKeyword() {return keyword;} and then do a command.getKeyword(); – Scorpion Jun 22 '12 at 05:22
  • I think the `execute` method inside the `load` method isn't override method. As you see the `execute` just have 1 parameter `String[] keyWord` and when you override you declare 2 parameters `Player player, String[] keyWord`. Please see in this [post](http://stackoverflow.com/questions/2047793/function-override-overload-in-java) for explanation. – Crazenezz Jun 22 '12 at 05:59
  • There is some questions I want to ask you regarding the code above: 1. What is the content of `String cmd[]`? As we can see the content based on the `input` that has been split, but I don't know what is the content of the `input`. 2. You using `get(int index)` why your input for the `get` method is String? – Crazenezz Jun 22 '12 at 06:09

4 Answers4

2

The problem is the get() method expects an int which represents the index of the list.

Harry Joy
  • 58,650
  • 30
  • 162
  • 207
Elliot Chance
  • 5,526
  • 10
  • 49
  • 80
0

How can I use the the get method to get the String from the Command class?

The simplest A simple way is to create getter method of keyWord in Command class and then

commands.get(index).getKeyWord(); //this will return the string

Here index is an integer variable indicating command class index in list. And don't forget to check for null. Get method would be something like

public String getKeyWord() {
    return this.keyWord;
}
Harry Joy
  • 58,650
  • 30
  • 162
  • 207
  • Well, the *simplest* method would be to make keyWord public. Nobody would recommend that - I certainly don't - but it's simpler than a getter. – Carl Manaster Jun 22 '12 at 05:33
  • @CarlManaster That would be simpler but not a good coding practice, I guess. Generally people avoids that. So we get getter as simpler and better way to do it :p – Harry Joy Jun 22 '12 at 05:36
0

Commands is an ArrayList and the get() function takes in a int Parameter.

Eithor use numbers or Use a Map<String,Command> for your code.

st0le
  • 33,375
  • 8
  • 89
  • 89
0

I guess you want the command that has the matching keyword, so try this:

Command command = null;
for (Command c : commands) {
    if (c.keyWord.equalsIgnoreCase(cmd[0])) {
        command = c;
        break;
    }
}

// command will not either be null or be the Command whose keyword matches


p.s. I recommend goolging for tutorials on java and OOP.

Bohemian
  • 412,405
  • 93
  • 575
  • 722