Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid Expj4 check if expression is invalid
    primarykey
    data
    text
    <p>I am using a library called expj4 on my code <a href="http://www.objecthunter.net/exp4j/" rel="nofollow">http://www.objecthunter.net/exp4j/</a>, everything works fine, but I am having a little problem if I use a expression that gives an error to the evaluation. </p> <p>Example if I use: 2 + 3 * ( 5 + ((36) . In this case I added an extra parenthesis and I would get an error and close the app. </p> <p>What I want to know if there is a way to check if the expression can be evaluated before give the result, so if the expression is wrong I can handle it. Here is the part of the code. </p> <pre><code>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); </code></pre> <p>Thank you!! :)</p> <p>LOGCAT Info:</p> <pre><code>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 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload