【发布时间】:2015-05-04 13:12:52
【问题描述】:
我正在尝试从用户那里获取一个数学表达式,但在这里我不断收到数字格式异常:
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)
这是我用来评估输入表达式的事件处理程序。文本字段应该接受 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.");
});
【问题讨论】:
-
除了你的正则表达式之外,
signs是什么? -
符号是我之前尝试创建的字符数组列表。我把它留在了代码中,但它目前不起作用。我还在 if 语句中尝试了 (temp.isEmpty()) ,但这也不起作用。
标签: java regex arraylist javafx linked-list