I'm trying to take in a mathematical expression from the user, but I keep getting a Number Format Exception here:
我正在尝试从用户那里获取数学表达式,但我在这里得到一个数字格式异常:
Exception in thread "JavaFX Application Thread" java.lang.NumberFormatException: For input string: "(13-1)*(12-10)"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:569)
at java.lang.Integer.valueOf(Integer.java:766)
at Main.lambda$start$2(Main.java:134)
at Main$$Lambda$73/16094097.handle(Unknown Source)
Here is my Event-Handler that I am using to evaluate the expression entered. The text field is supposed to take in an expression of 4 numbers (1-13) and evaluate if it equals 24. I'm using a regular expression but it doesn't seem to be working. Also, I've got a character array that I was using at first for just the signs but that seems to not be necessary. I am very new to regex and have tried a number of combinations already.
这是我用来评估输入表达式的事件处理程序。文本字段应该采用4个数字(1-13)的表达式并评估它是否等于24.我使用正则表达式但它似乎不起作用。另外,我有一个字符数组,我最初使用的只是符号,但似乎没有必要。我是正则表达式的新手,已经尝试了很多组合。
btVerify.setOnAction(
(ActionEvent e) ->
{
LinkedList<Character> expInput = new LinkedList<Character>();
for(char c: tfExpress.getText().toCharArray()){
expInput.add(c);
}
String[] inputIntegers = tfExpress.getText().split("[^0-9]+-/*()");
expInput.removeIf(p-> p.equals(signs));
ArrayList<Integer> temp = new ArrayList<>();
for(String s:inputIntegers)
{
temp.add(new Integer(Integer.valueOf(s)));
}
temp.remove(new Integer(card1.CardValue()));
temp.remove(new Integer(card2.CardValue()));
temp.remove(new Integer(card3.CardValue()));
temp.remove(new Integer(card4.CardValue()));
if(temp.isEmpty()&& expInput.isEmpty())
{
if(express == 24){
display.setText("Correct");
}
else
display.setText("Incorrect");
}
else
display.setText("The numbers in the expression don't "
+ "match the numbers in the set.");
});
2 个解决方案
#1
The NumberFormat Exception
is because your regex doesn't separate the numbers from the signs/literals.
NumberFormat异常是因为您的正则表达式不会将数字与符号/文字分开。
tfExpress.getText().split("[^0-9]+-/*()");
returns the whole text i.e. (13-1)*(12-10)
返回全文,即(13-1)*(12-10)
You need a more complex regex expression which will separate the signs from the numbers. Thanks to @unihedron for the regex expression.
您需要一个更复杂的正则表达式,它将符号与数字分开。感谢@unihedron的正则表达式。
\b|(?<=[()])(?=[^\d()])|(?<=[^\d()])(?=[()])
Now you can use
现在你可以使用了
...
String regex = "\b|(?<=[()])(?=[^\d()])|(?<=[^\d()])(?=[()])";
tfExpress.getText().split(regex);
...
A very simple working example can be found here.
这里可以找到一个非常简单的工作示例。
#2
I hope you don't expect to evaluate that formula with regex. That won't work.
我希望你不要期望用正则表达式来评估这个公式。那不行。
For the splitting, if you don't know regex, use something else like the StringTokenizer.
对于拆分,如果你不知道正则表达式,请使用其他类似StringTokenizer的东西。
StringTokenizer t = new StringTokenizer( "(13-1)*(12-10)", "+-/*()", true);
while( t.hasMoreTokens()) {
System.out.println(t.nextToken());
}
results in
(
13
-
1
)
*
(
12
-
10
)
#1
The NumberFormat Exception
is because your regex doesn't separate the numbers from the signs/literals.
NumberFormat异常是因为您的正则表达式不会将数字与符号/文字分开。
tfExpress.getText().split("[^0-9]+-/*()");
returns the whole text i.e. (13-1)*(12-10)
返回全文,即(13-1)*(12-10)
You need a more complex regex expression which will separate the signs from the numbers. Thanks to @unihedron for the regex expression.
您需要一个更复杂的正则表达式,它将符号与数字分开。感谢@unihedron的正则表达式。
\b|(?<=[()])(?=[^\d()])|(?<=[^\d()])(?=[()])
Now you can use
现在你可以使用了
...
String regex = "\b|(?<=[()])(?=[^\d()])|(?<=[^\d()])(?=[()])";
tfExpress.getText().split(regex);
...
A very simple working example can be found here.
这里可以找到一个非常简单的工作示例。
#2
I hope you don't expect to evaluate that formula with regex. That won't work.
我希望你不要期望用正则表达式来评估这个公式。那不行。
For the splitting, if you don't know regex, use something else like the StringTokenizer.
对于拆分,如果你不知道正则表达式,请使用其他类似StringTokenizer的东西。
StringTokenizer t = new StringTokenizer( "(13-1)*(12-10)", "+-/*()", true);
while( t.hasMoreTokens()) {
System.out.println(t.nextToken());
}
results in
(
13
-
1
)
*
(
12
-
10
)