java - Android Expj4 check if expression is invalid -


i using library called expj4 on code http://www.objecthunter.net/exp4j/, works fine, having little problem if use expression gives error evaluation.

example if use: 2 + 3 * ( 5 + ((36) . in case added parenthesis , error , close app.

what want know if there way check if expression can evaluated before give result, if expression wrong can handle it. here part of 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); 

thank you!! :)

logcat info:

07-10 23:58:50.781: w/system.err(9496): de.congrace.exp4j.unparsableexpressionexception: there 2 unmatched parantheses in expression ' 65 * 69 *  (98 / sin( (36) ' 07-10 23:58:50.781: w/system.err(9496):     @ de.congrace.exp4j.tokenizer.gettokens(tokenizer.java:185) 07-10 23:58:50.781: w/system.err(9496):     @ de.congrace.exp4j.rpnconverter.torpnexpression(rpnconverter.java:63) 07-10 23:58:50.781: w/system.err(9496):     @ de.congrace.exp4j.expressionbuilder.build(expressionbuilder.java:276) 07-10 23:58:50.781: w/system.err(9496):     @ com.gabilheri.formulacalculator.scientificactivity$16.onclick(scientificactivity.java:273) 07-10 23:58:50.781: w/system.err(9496):     @ android.view.view.performclick(view.java:4102) 07-10 23:58:50.781: w/system.err(9496):     @ android.view.view$performclick.run(view.java:17126) 07-10 23:58:50.781: w/system.err(9496):     @ android.os.handler.handlecallback(handler.java:615) 07-10 23:58:50.781: w/system.err(9496):     @ android.os.handler.dispatchmessage(handler.java:92) 07-10 23:58:50.781: w/system.err(9496):     @ android.os.looper.loop(looper.java:155) 07-10 23:58:50.781: w/system.err(9496):     @ android.app.activitythread.main(activitythread.java:5536) 07-10 23:58:50.791: w/system.err(9496):     @ java.lang.reflect.method.invokenative(native method) 07-10 23:58:50.791: w/system.err(9496):     @ java.lang.reflect.method.invoke(method.java:511) 07-10 23:58:50.791: w/system.err(9496):     @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1074) 07-10 23:58:50.791: w/system.err(9496):     @ com.android.internal.os.zygoteinit.main(zygoteinit.java:841) 07-10 23:58:50.791: w/system.err(9496):     @ dalvik.system.nativestart.main(native method) 07-10 23:58:50.791: w/dalvikvm(9496): threadid=1: thread exiting 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):     @ com.gabilheri.formulacalculator.scientificactivity$16.onclick(scientificactivity.java:286) 07-10 23:58:50.801: e/androidruntime(9496):     @ android.view.view.performclick(view.java:4102) 07-10 23:58:50.801: e/androidruntime(9496):     @ android.view.view$performclick.run(view.java:17126) 07-10 23:58:50.801: e/androidruntime(9496):     @ android.os.handler.handlecallback(handler.java:615) 07-10 23:58:50.801: e/androidruntime(9496):     @ android.os.handler.dispatchmessage(handler.java:92) 07-10 23:58:50.801: e/androidruntime(9496):     @ android.os.looper.loop(looper.java:155) 07-10 23:58:50.801: e/androidruntime(9496):     @ android.app.activitythread.main(activitythread.java:5536) 07-10 23:58:50.801: e/androidruntime(9496):     @ java.lang.reflect.method.invokenative(native method) 07-10 23:58:50.801: e/androidruntime(9496):     @ java.lang.reflect.method.invoke(method.java:511) 07-10 23:58:50.801: e/androidruntime(9496):     @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1074) 07-10 23:58:50.801: e/androidruntime(9496):     @ com.android.internal.os.zygoteinit.main(zygoteinit.java:841) 07-10 23:58:50.801: e/androidruntime(9496):     @ 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 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 

got answer on stack overflow chat!! here how solved problem :)

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

Comments

Popular posts from this blog

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -