java.util.regex.PatternSyntaxException:在索引 0 + 附近悬空元字符 '+'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40246231/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java.util.regex.PatternSyntaxException: Dangling meta character '+' near index 0 +
提问by Jarred Parr
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
当我启动 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;
}
采纳答案by Paulo
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("\\+")
.
Regex 中有保留字符,您应该转义这些字符以实现您想要的。例如,您不能使用String.split("+")
,您必须使用String.split("\\+")
。
The correct operators would be:
正确的运算符是:
String[] operators = new String[] {"-","\+","/","\*","x","\^","X"};
回答by j.seashell
Change: String[] split = nums.split(operator);
改变: String[] split = nums.split(operator);
To this: String[] split = nums.split("\\" + operator);
对此: 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
编辑:这仅适用于标准运算符,而不适用于 x 或 X。您必须String[] operators
实际更改您的声明,就像提到的其他答案一样。虽然个人,我会做一些输入验证,并做了替换()上x
或X
可*
替代
回答by wlaem
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. "\\+"
split() 方法使用正则表达式。'+' 符号是正则表达式中的特殊字符,因此您需要使用反斜杠符号 ('\') 对其进行转义。但是你还需要转义java中的反斜杠符号,所以你需要两个反斜杠,例如“\\+”
回答by prabath
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 "\\+"
"\\*"
"\\^"
在您的情况下,+
*
并被^
视为具有特殊含义,通常称为元字符。String.split()
方法将正则表达式作为其参数并返回一个String
数组。为避免将上述视为元字符,您需要在代码中使用这些转义序列"\\+"
"\\*"
"\\^"
modify your operator array like this
像这样修改您的运算符数组
private String[] operators = new String[] {"-","\+","/","\*","x","\^","X"};
for more detalis refere these links regex.Patternand String.split()
有关更多详细信息,请参阅这些链接regex.Pattern和 String.split()
回答by saci nassim
you can use case String.valueOf('+');
你可以使用 case String.valueOf('+');