1

我在我的代码http://www.objecthunter.net/exp4j/上使用了一个名为 expj4 的库,一切正常,但是如果我使用一个给评估带来错误的表达式,我会遇到一个小问题。

例如,如果我使用: 2 + 3 * ( 5 + ((36) 。在这种情况下,我添加了一个额外的括号,我会收到错误并关闭应用程序。

我想知道是否有办法在给出结果之前检查表达式是否可以被评估,所以如果表达式错误我可以处理它。这是代码的一部分。

Calculable calc = null;
            try {
                double varPi = Math.PI;

                if (degRad == false) {
                    calc = new ExpressionBuilder(txt)
                            .withCustomFunction(cosdFunc)
                            .withCustomFunction(tandFunc)
                            .withCustomFunction(sindFunc)
                            .withCustomFunction(funX)
                            .withVariable("π", varPi)
                            .withVariable("x", varX).build();
                } else {
                    calc = new ExpressionBuilder(txt)
                            .withCustomFunction(funX)
                            .withVariable("π", varPi)
                            .withVariable("x", varX).build();
                }
            } catch (UnknownFunctionException e) {
                e.printStackTrace();
            } catch (UnparsableExpressionException e) {
                e.printStackTrace();
            }

            double result1 = calc.calculate();
            String result = Double.toString(result1);
            txtCalc.setText(result);

谢谢!!:)

LOGCAT 信息:

07-10 23:58:50.781: W/System.err(9496): de.congrace.exp4j.UnparsableExpressionException: There are 2 unmatched parantheses in expression ' 65 * 69 *  (98 / sin( (36) '
07-10 23:58:50.781: W/System.err(9496):     at de.congrace.exp4j.Tokenizer.getTokens(Tokenizer.java:185)
07-10 23:58:50.781: W/System.err(9496):     at de.congrace.exp4j.RPNConverter.toRPNExpression(RPNConverter.java:63)
07-10 23:58:50.781: W/System.err(9496):     at de.congrace.exp4j.ExpressionBuilder.build(ExpressionBuilder.java:276)
07-10 23:58:50.781: W/System.err(9496):     at com.gabilheri.formulacalculator.ScientificActivity$16.onClick(ScientificActivity.java:273)
07-10 23:58:50.781: W/System.err(9496):     at android.view.View.performClick(View.java:4102)
07-10 23:58:50.781: W/System.err(9496):     at android.view.View$PerformClick.run(View.java:17126)
07-10 23:58:50.781: W/System.err(9496):     at android.os.Handler.handleCallback(Handler.java:615)
07-10 23:58:50.781: W/System.err(9496):     at android.os.Handler.dispatchMessage(Handler.java:92)
07-10 23:58:50.781: W/System.err(9496):     at android.os.Looper.loop(Looper.java:155)
07-10 23:58:50.781: W/System.err(9496):     at android.app.ActivityThread.main(ActivityThread.java:5536)
07-10 23:58:50.791: W/System.err(9496):     at java.lang.reflect.Method.invokeNative(Native Method)
07-10 23:58:50.791: W/System.err(9496):     at java.lang.reflect.Method.invoke(Method.java:511)
07-10 23:58:50.791: W/System.err(9496):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1074)
07-10 23:58:50.791: W/System.err(9496):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:841)
07-10 23:58:50.791: W/System.err(9496):     at dalvik.system.NativeStart.main(Native Method)
07-10 23:58:50.791: W/dalvikvm(9496): threadid=1: thread exiting with uncaught exception (group=0x41e2c540)
07-10 23:58:50.801: E/AndroidRuntime(9496): FATAL EXCEPTION: main
07-10 23:58:50.801: E/AndroidRuntime(9496): java.lang.NullPointerException
07-10 23:58:50.801: E/AndroidRuntime(9496):     at com.gabilheri.formulacalculator.ScientificActivity$16.onClick(ScientificActivity.java:286)
07-10 23:58:50.801: E/AndroidRuntime(9496):     at android.view.View.performClick(View.java:4102)
07-10 23:58:50.801: E/AndroidRuntime(9496):     at android.view.View$PerformClick.run(View.java:17126)
07-10 23:58:50.801: E/AndroidRuntime(9496):     at android.os.Handler.handleCallback(Handler.java:615)
07-10 23:58:50.801: E/AndroidRuntime(9496):     at android.os.Handler.dispatchMessage(Handler.java:92)
07-10 23:58:50.801: E/AndroidRuntime(9496):     at android.os.Looper.loop(Looper.java:155)
07-10 23:58:50.801: E/AndroidRuntime(9496):     at android.app.ActivityThread.main(ActivityThread.java:5536)
07-10 23:58:50.801: E/AndroidRuntime(9496):     at java.lang.reflect.Method.invokeNative(Native Method)
07-10 23:58:50.801: E/AndroidRuntime(9496):     at java.lang.reflect.Method.invoke(Method.java:511)
07-10 23:58:50.801: E/AndroidRuntime(9496):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1074)
07-10 23:58:50.801: E/AndroidRuntime(9496):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:841)
07-10 23:58:50.801: E/AndroidRuntime(9496):     at dalvik.system.NativeStart.main(Native Method)
07-10 23:58:50.841: D/libc(9496): [NET] getaddrinfo  hn 24, servname NULL, ai_family 0+
07-10 23:58:50.841: D/libc(9496): [NET] ht 0x73736c2e676f6f
07-10 23:58:50.851: D/libc(9496): [NET] getaddrinfo-exit,err=8
07-10 23:58:50.851: D/libc(9496): [NET] getaddrinfo  hn 24, servname NULL, ai_family 0+
07-10 23:58:50.851: D/libc(9496): [NET] ht 0x73736c2e676f6f
07-10 23:58:50.851: D/libc(9496): [NET] android_getaddrinfo_proxy+
07-10 23:58:50.851: D/libc(9496): [NET]android_getaddrinfo_proxy+
07-10 23:58:50.871: D/libc(9496): [NET]android_getaddrinfo_proxy-,1
07-10 23:58:50.871: D/libc(9496): [NET] getaddrinfo-exit 1
07-10 23:58:50.871: I/global(9496): call createSocket() return a new socket.
07-10 23:58:50.871: D/libc(9496): [NET] getaddrinfo  hn 14, servname NULL, ai_family 0+
07-10 23:58:50.871: D/libc(9496): [NET] ht 0x37342e3132352e
07-10 23:58:50.871: D/libc(9496): [NET] getaddrinfo-exit SUCCESS
4

1 回答 1

2

在堆栈溢出聊天中得到了我的答案!这是我解决问题的方法:)

try {
    double result1 = calc.calculate();
    String result = Double.toString(result1);
    txtCalc.setText(result);
    if (varX != 0) {
    btnFunX.setText("f(" + Double.toString((varX * 100) / 100) + ")");
        } catch (Exception e) {
            txtCalc.setText("Error!");      
                }
于 2013-07-11T14:59:30.497 回答