Nathan1995
Nathan1995

Reputation: 111

Validation error in code

I am experiencing trouble in the creation of my reverse polish notation calculator with my validation code. I need the calculator to accept the two shift operators (<< and >>) as part of the calculations. The following snippets of code is the validation part and also the calculation.

public static boolean isInt(String userinput) {
    try {
        Integer.parseInt(userinput); // Try to parse. Makes sure that the values entered are actual numbers
        return true; // Boolean value to show if the equation entered is valid or not
    } catch (NumberFormatException e) {
        System.out.println("Please enter a  valid expression!");
        invalidlines++;
        return false;
    }
}

public static boolean isValidLine(String line) {
    line = line.trim();
    if (line.length() <= 4) { // Trims the lines down to 4 and ensures there is no spaces being included
        return false;
    } else {
        String[] calcarray = new String[3];
        calcarray = line.split(" ");
        String operators = new String("[+\\-\\*\\/\\<<\\>>\\%\\&\\|]"); // Validator using regular expressions to check the operator used
        if (isInt(calcarray[0].toString()) && isInt(calcarray[1].toString()) && calcarray[2].matches(operators)) { // Checks that the operator in the string matches the ones in the regular expression 
            return true;
        } else {
            return false;
        }
    }
}

below is the calculator part:

String keyboardInput = new String();
Scanner kbScan = new Scanner(System.in);
int answer = 0;
while (true) {
    display("Please enter an equation");
    keyboardInput = kbScan.nextLine();
    if (isValidLine(keyboardInput)) {
        String[] equation = new String[3];  // We know that this is only going to contain 3 to be valid
        equation = keyboardInput.split(" "); // split this up, as it's stored with the spaces.
        int num1 = Integer.parseInt(equation[0]);
        int num2 = Integer.parseInt(equation[1]);
        switch (equation[2]) { // This case switch checks the third position of the 
            // string to decide which operator is being used. It then works out the 
            // answer and breaks to the next instruction
            case ("+"):
                answer = num1 + num2;
                break;
            case ("-"):
                answer = num1 - num2;
                break;
            case ("/"):
                answer = num1 / num2;
                break;
            case ("*"):
                answer = num1 * num2;
                break;
            case ("<<"):
                answer = num1 << num2;
                break;
            case (">>"):
                answer = num1 >> num2;
                break;
            case ("%"):
                answer = num1 % num2;
                break;
            case ("|"):
                answer = num1 | num2;
                break;
            case ("&"):
                answer = num1 & num2;
                break;
        }
        display("Your post fix expression: " + equation[0] + " " + equation[1] + " " + equation[2]);
        display("Your calculation: " + equation[0] + " " + equation[2] + " " + equation[1] + " = " + answer);
    } else {
        display("The equation you entered is invalid");
    }
}

Whenever a valid expression is entered the following error is shown in the console:

Enter F for file calculator or K for keyboard input
k
Please enter an equation
10 2 <<
The equation you entered is invalid
Please enter an equation

And I cannot figure out which part of my validation is wrong for these expressions.

Upvotes: 1

Views: 60

Answers (1)

Matt
Matt

Reputation: 1308

Problem is with your operators regex. User rather something like:

("\\+|\\-|\\*|\\/|<<|>>|\\%|\\&|\\|")

Upvotes: 1

Related Questions