gpt4 book ai didi

java - Android Expj4 检查表达式是否无效

转载 作者:行者123 更新时间:2023-11-29 21:39:10 25 4
gpt4 key购买 nike

我在我的代码中使用了一个名为 expj4 的库 http://www.objecthunter.net/exp4j/ ,一切正常,但如果我使用的表达式会导致评估出错,我就会遇到一点问题。

例如,如果我使用: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

最佳答案

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

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

关于java - Android Expj4 检查表达式是否无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17583921/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com