I am trying to figure out a way to parse the user input. There are commands that are one word, two words and three words long. Somehow I have to check now whether the user has entered an existing command and then pass on the arguments (all words after the command) – getArguments() does that for me.
How can I use Stream or anything else to find the entered command?
String input = Terminal.readLine(); final List<Command> commands = parser.initialiseCommands(register); final List<String> inputSplit = Arrays.asList(input.split(" ")); final Command command = commands.stream() .filter(cmd -> cmd.getName().equals(???) .findAny() .orElseThrow(() -> new InvalidInputException("no such command"); final List<String> arguments = parser.getArguments(inputSplit, command); command.setArguments(arguments); command.execute();
initialiseCommands() holds all registered Commands. Every command has the getName() method which returns a String of the command name (e. g. for the add track command it’s “add track”).
Here are some commands of different length:
- "add track <point> -> <point>" - length 2 - "quit" - length 1 - "delete rolling stock <id>" - length 3 - "step <speed>" - length 1
Can someone please show me a way of how to do this? If any information is missing, please tell me. I’ve been trying to figure this out for hours.