代码中的验证错误

时间:2015-12-06 16:18:01

标签: java regex

我在使用验证码创建反向抛光表示法计算器时遇到了麻烦。作为计算的一部分,我需要计算器接受两个移位运算符(<<>>)。以下代码片段是验证部分以及计算。

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;
        }
    }
}
下面的

是计算器部分:

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");
    }
}

每当输入有效表达式时,控制台中都会显示以下错误:

  

输入F表示文件计算器,或输入K表示键盘输入
  ķ
  请输入公式
  10 2&lt;
  您输入的等式无效
  请输入等式

我无法弄清楚我的验证哪部分是错误的。

1 个答案:

答案 0 :(得分:1)

问题在于您的operators正则表达式。 用户更像是:

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