delete scanners last input to not repeat the last order

Viewed 26

I have the following problem. This is my parser class. it runs smooth and ignores invalide inputs. But if hit enter in the gui and then make a space and delete this space it runs the last command again... is there any way to ignore this and not performe the same movement again. For example if I write forward 5 then hit enter it moves forward 5. but if i then add a space in the next line and delete this it repeats the forward 5. Where and how sould I clear the carProgram string. any help please its for a grades school project thanks guys

/**
 * Class CommandParser: 
 * Responsible for filtering all kind of wrong commands and converting the full command into Array of strings,
 * containing all the independent parts of the possible commanding.
 * The input is a String which contains the whole TextArea from the GUI in {@link CarRenderer}
 * This Class is exclusively called by {@link BoardMaker} and returns an Array of strings, containing a direction and a amount of steps,
 * also a second step variable in case of a drive command
 * @author Jan Ellenberger 
 * @author Vithusan Ramalingam
 * @author Thirith Yang
 *
 */

import java.util.Scanner;


public class CommandParser {
/**
     * Precondition: checks that we have received a command
     * 
     * The function checks if the CarProgram contains only one command, then creates the list commands with separated direction and step string. 
     * Another step (position) String if the command is "drive".
     * 
     * Postcondition: checks that commands is not null
     * 
     * @param carProgram: is the Car-program command from the GUI command line
     * @return commands: list of separated direction and step string
     */

    public String[] parseString(String carProgram){
        //precondition
        assert(carProgram != null);
        //
        String commandMove = "";
        String commandTurn = "";
        String commandMoveDrive = "";
        String inputCommand = "";
        String[] seperatedCommands;
        int carProgramLength = carProgram.length();
        //Scan & filter the program input
        if(carProgram.contains("\n") && carProgram.substring(carProgram.length()-1, carProgramLength).contentEquals("\n")) {
            //find the correct whole command (direction and amount of steps)
            String wholeCommand = "";
            Scanner scn = new Scanner(carProgram).useDelimiter("\n");
            while(scn.hasNext()){
                wholeCommand = scn.next();
            }

            //Separate direction from the amount of steps
            seperatedCommands = wholeCommand.split(" ");
            //if we dont have any amount of steps, check if its right or left
            if(seperatedCommands.length == 1) {
                if(wholeCommand.contentEquals("right")) {
                    inputCommand = this.inputFilter("right");
                    commandMove = "0";
                    String[] commands = {inputCommand, commandMove, commandMoveDrive};
                    return commands;
                }
                else if(wholeCommand.contentEquals("left")) {
                    inputCommand = this.inputFilter("left");
                    commandMove = "0";
                    String[] commands = {inputCommand, commandMove, commandMoveDrive};
                    return commands;
                }
                else {
                    String[] breaker = {""};
                    return breaker;
                }
            }

            //Extra rule for drive commands and their spaces
            if(seperatedCommands[0].contentEquals("drive") && seperatedCommands.length == 3) {
                seperatedCommands[1] = seperatedCommands[seperatedCommands.length -2];
                seperatedCommands[2] = seperatedCommands[seperatedCommands.length -1];
            }
            //if there are extra spaces do this to ignore them
            else if(seperatedCommands.length > 3) {
                seperatedCommands[1] = seperatedCommands[seperatedCommands.length -1];
            }
            //Validates and gets the direction with {@see inputFilter}
            inputCommand = this.inputFilter(seperatedCommands[0].replaceAll(" ", ""));
            //Get the amount of steps
            commandMove = seperatedCommands[1];
            //get an extra step-variable in case of the drive scenario
            if(inputCommand.contentEquals("drive")){
                if (seperatedCommands.length >= 3){
                commandMoveDrive = seperatedCommands[2];
                String[] commands = {inputCommand, commandMove, commandMoveDrive};
                return commands;
                }
                else {
                    String[] breaker = {""};
                    return breaker;
                }
            }
        }

        String[] commands = {inputCommand, commandMove};
        //Postcondition
        assert(commands[0]!= null);
        return commands;
    }


    public String inputFilter(String carProg){
        //Precondition
        assert( !carProg.equals("") && !carProg.equals(null));
        Scanner commandScn = new Scanner(carProg);
        String command="";
        while(command.contentEquals("")) {
            switch(commandScn.nextLine()){
                case "right":
                    return "right";

                case "left":
                    return "left";

                case "forward":
                    return "forward";

                case "backward":
                    return "backward";

                case "drive":
                    return "drive";
            }
            return command;
        }
        //Postcondition
        assert( !carProg.equals("") && !carProg.equals(null));
        return command;
    }
}```
0 Answers
Related