【问题标题】:java.util.regex.PatternSyntaxException: Dangling meta character '+' near index 0 +java.util.regex.PatternSyntaxException:在索引 0 + 附近悬空元字符“+”
【发布时间】:2017-03-07 20:56:26
【问题描述】:

我在启动 UI 时遇到错误,导致此代码在标题中向我吐出错误。它适用于我的所有其他运算符符号,所以我真的不确定这里发生了什么。我不想发布所有代码,如果这还不够,你可以在我的 gitHub 上找到其余代码:https://github.com/jparr721/Calculator-App/tree/master/src/calculator

public class Calculation_Controls {

    public double A, B;

    private String[] operators = new String[] {"-","+","/","*","x","^","X"};


    /**
     * Check for the symbol being used within the TextArea to then
     * apply the correct caculation method.
     * FIXME - Allow for multiple symbols to be used and have them return
     * FIXME - a result in accordance with PEMDAS
     *
     *@param nums
     *
     * @return operator, or error
     */
    public String findSymbol(String nums) {

        for (String operator : operators) {
            if (nums.contains(operator)) {
                return operator;
            }
        }
        return "invalid input";
    }

    /**
     * Input method to take the user input from the text area
     * and apply the correct calculation to it
     *
     * @param nums - Stores the input as a String which I then convert to an int
     *             then back to a string to be printed to the TextArea
     *
     * @return - The result of the calculation as a string
     */
    public String input(String nums){

        String operator = findSymbol(nums);
        if (operator == null){
            System.out.println("Invalid input");

        }
        String[] split = nums.split(operator);
        int left = Integer.parseInt(split[0]);
        int right = Integer.parseInt((split[1]));
        String result = "";

        switch (operator){

            case "+":
                result = Double.toString(add(left, right));
                break;
            case "-":
                result = Double.toString(subtract(left, right));
                break;
            case "*":
            case "x":
            case "X":
                result = Double.toString(multiply(left, right));
                break;
            case "/":
                result =  Double.toString(divide(left, right));
                break;
            case "^":
                result =  Double.toString(pwr(left, right));
                break;
            default:
                System.out.println("Invalid Operator");
        }
        return result;
    }

【问题讨论】:

标签: java regex


【解决方案1】:

Regex 中有保留字符,您应该对这些字符进行转义以实现您想要的。例如,你不能使用String.split("+"),你必须使用String.split("\\+")

正确的运算符是:

String[] operators = new String[] {"-","\\+","/","\\*","x","\\^","X"};

【讨论】:

【解决方案2】:

在您的情况下,+ *^ 具有特殊含义,通常称为元字符。 String.split() 方法将一个正则表达式作为其参数并返回一个 String 数组。为避免将上述内容视为元字符,您需要在代码中使用这些转义序列 "\\+" "\\*" "\\^"

像这样修改你的操作符数组

private String[] operators = new String[] {"-","\\+","/","\\*","x","\\^","X"};

更多细节参考这些链接regex.PatternString.split()

【讨论】:

  • 这不起作用,您对可能导致此问题的原因还有其他想法吗?
【解决方案3】:

你可以使用 case String.valueOf('+');

【讨论】:

  • 您的大多数答案似乎都遭到了很多反对。在尝试发布答案之前,请阅读帮助部分中的 How do I write a good answer? 文章。
【解决方案4】:

更改:String[] split = nums.split(operator);

对此:String[] split = nums.split("\\" + operator);

编辑:这仅适用于标准运算符,而不适用于 x 或 X。您实际上必须更改 String[] operators 声明,就像提到的其他答案一样。不过,就我个人而言,我会进行某种输入验证,并在 xX 上执行 replace() 以改为 *

【讨论】:

    【解决方案5】:

    split() 方法使用正则表达式。 '+' 符号是正则表达式中的特殊字符,因此您需要使用反斜杠符号 ('\') 对其进行转义。但是你还需要在java中转义反斜杠符号,所以你需要两个反斜杠,例如"\\+"

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多