59

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;
    }
locke14
  • 1,335
  • 3
  • 15
  • 36
Jarred Parr
  • 1,167
  • 3
  • 11
  • 24

5 Answers5

127

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("\\+").

The correct operators would be:

String[] operators = new String[] {"-","\\+","/","\\*","x","\\^","X"};
Alex Weitz
  • 3,199
  • 4
  • 34
  • 57
Paulo
  • 2,956
  • 3
  • 20
  • 30
  • 1
    Thanks! In my case doing `$myString.split([+])` according to this: https://stackoverflow.com/a/21357226/1057052 worked! (Using Apache VTL with AppSync) – Jose A Sep 21 '19 at 03:18
10

in your case + * and ^ are treated with a special meaning, most often called as Metacharacters. String.split() method takes a regex expression as its argument and return a String array. To avoid treating above as a Metacharacters you need to use these escape sequences in your code "\\+" "\\*" "\\^"

modify your operator array like this

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

for more detalis refere these links regex.Pattern and String.split()

prabath
  • 143
  • 7
4

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

  • Most of your answers seem to be getting a lot of downvotes. Please read the [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer) article in the Help Section before attempting to post an answer. – Zephyr Jul 10 '19 at 20:45
1

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

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

edit: This will only work for standard operators, not the x or X. You'll have to change your String[] operators declaration actually, like the other answer mentioned. Personally though, I'd do some kind of input validation and do a replace() on x or X to be * instead

jseashell
  • 745
  • 9
  • 19
1

the split() method uses regex. the '+' symbol is a special character in regex, so you need to escape it with the backslash symbol ('\'). But you also need to escape the backslash symbol in java, so you need two backslashes, e.g. "\\+"

wlaem
  • 304
  • 1
  • 3
  • 13