1

First, appologies for the lack of a better title. Hopefully someone with more experience in Java is able to change it to something more appropiate.

So I'm faced with the following exception:

The method copyPartialMatches(String, Iterable, T) in the type StringUtil is not applicable for the arguments (String, String[], List)

The documentation for this method states:

Parameters:
token - String to search for
originals - An iterable collection of strings to filter.
collection - The collection to add matches to

My code:

public class TabHandler implements TabCompleter {
    private static final String[] params = {"help", "menu", "once", "repeat", "infinite", "cancel"};

    @Override
    public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) {
        final List<String> completions = new ArrayList<>();
        StringUtil.copyPartialMatches(args[0], params, completions);

        Collections.sort(completions);
        return completions;
    }
}

I'm fairly certain the problem lies with the completions List. Perhaps this is not a valid collection? It was my understanding that it was, but right now I'm just at a loss here. So hopefully you guys can help me out.

0

3 Answers 3

2

Try passing in an actual List as the second parameter to StringUtil#copyPartialMatches:

@Override
public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) {
    final List<String> completions = new ArrayList<>();
    StringUtil.copyPartialMatches(args[0], Arrays.asList(params), completions);
    //                                     ^^^ change is here

    Collections.sort(completions);
    return completions;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! That solved the issue straight away ^^ Will accept when able to :)
1

String[] isn't Iterable. Change

StringUtil.copyPartialMatches(args[0], params, completions);

to pass a List<String> instead. Something like,

StringUtil.copyPartialMatches(args[0], Arrays.asList(params), completions);

2 Comments

Thanks, this indeed solved the issue. I will accept Tim Biegeleisen's answer because he was slightly faster. But both your answers are equaly as good :)
@icecub No problem. Glad the issue was resolved.
1

Why is an array not assignable to Iterable?

Arrays don't implement the Iterable interface. That's why the method signature isn't matching. Converting the array to a list works since the latter implements the interface.

The forEach loop for arrays is a special case (arrays don't implement the Iterable<T> interface yet the work with the forEach loop).

1 Comment

Thanks, I'm sure this information will be usefull to future readers

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.