java.util.regex.PatternSyntaxException:在索引0 +附近晃来晃去的元字符'+' [英] java.util.regex.PatternSyntaxException: Dangling meta character '+' near index 0 +

查看:47
本文介绍了java.util.regex.PatternSyntaxException:在索引0 +附近晃来晃去的元字符'+'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

启动UI时出现错误,导致此代码在标题中向我吐出错误.它适用于我所有其他运算符,所以我真的不确定这里发生了什么.我不想发布所有代码,因此如果我的gitHub不够用,您可以找到其余代码:

I am getting the error when I launch my UI that causes this code to spit the error at me in the title. It works for all of my other operator symbols so I am really not sure what's going on here. I didn't want to post all the code so you can find the rest if this isn't enough on my 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;
    }

推荐答案

正则表达式中有保留字符,您应该对这些字符进行换码以实现所需的功能.例如,您不能使用 String.split("+"),而必须使用 String.split("\\ +").

There are reserved character in Regex and you should scape these character to achieve what you want. For example, you can't use String.split("+"), you have to use String.split("\\+").

正确的运算符将是:

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

这篇关于java.util.regex.PatternSyntaxException:在索引0 +附近晃来晃去的元字符'+'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆