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.

icecub
  • 8,615
  • 6
  • 41
  • 70

3 Answers3

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;
}
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
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);
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
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).

Prashant Pandey
  • 4,332
  • 3
  • 26
  • 44