java.util.regex.PatternSyntaxException: Dangling meta character '+' near index 0 +
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"};
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()