找到你要的答案

Q:Number Format Exception and Regex Inquiry

Q:数字格式异常和正则表达式查询

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.

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.");
            });

我试图从用户的数学表达式,但我一直在这里得到一个数字格式的例外:

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.");
            });
answer1: 回答1:

The NumberFormat Exception is because your regex doesn't separate the numbers from the signs/literals.

tfExpress.getText().split("[^0-9]+-/*()"); 

returns the whole text i.e. (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.

\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.

NumberFormat例外是因为你的正则表达式不单独的数字标志/文字。

tfExpress.getText().split("[^0-9]+-/*()"); 

返回整个文本即(13-1)*(12-10)

你需要一个更复杂的正则表达式,表达式将单独的迹象从数。感谢@ unihedron的正则表达。

\b|(?<=[()])(?=[^\d()])|(?<=[^\d()])(?=[()])

现在你可以使用

...
String regex = "\b|(?<=[()])(?=[^\d()])|(?<=[^\d()])(?=[()])";
tfExpress.getText().split(regex);
... 

一个非常简单的工作例子可以在这里找到。

answer2: 回答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 t = new StringTokenizer( "(13-1)*(12-10)", "+-/*()", true);
while( t.hasMoreTokens()) {
  System.out.println(t.nextToken());
}

results in

(
13
-
1
)
*
(
12
-
10
)

我希望你不要指望用正则表达式评估公式。那行不通。

对劈裂,如果你不知道正则表达式,用别的东西像stringtokenizer。

StringTokenizer t = new StringTokenizer( "(13-1)*(12-10)", "+-/*()", true);
while( t.hasMoreTokens()) {
  System.out.println(t.nextToken());
}

结果

(
13
-
1
)
*
(
12
-
10
)
java  regex  arraylist  javafx  linked-list