Java.util.regex.PatternSyntaxException: Dangling metacharacter "+" next to index 0 +

When I launch my user interface, I get an error message that causes this code to throw an error in the header. This works for all my other operator characters, so I'm really not sure what is going on here. I didn’t want to publish all the code so you could find the rest if this is not 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;
    }
+11
source share
5 answers

Regex , . , String.split("+"), String.split("\\+").

:

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

+ * ^ , . String.split() regex String. , escape- "\\+" "\\*" "\\^"

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

regex.Pattern String.split()

+3

split() regex. '+' , ('\'). java, , . "\\ +"

+1

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

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

edit: , x X. String[] operators , . , () x x *

0

you can use case String.valueOf ('+');

0
source

Source: https://habr.com/ru/post/1658850/


All Articles