3

I want to implement the following code in Android. How do I do that?

System.out.println("Enter a String");
Scanner sc = new Scanner(System.in);
String string = sc.next();

System.out.println(new ScriptEngineManager().getEngineByName("JavaScript").eval(string));//this is solving infix expression using javascript
tripleee
  • 175,061
  • 34
  • 275
  • 318
user2475511
  • 79
  • 1
  • 7
  • The Android API, doesn't provide such a class. However, you could simply implement your own stack to evaluated an expression. See the second answer of this : http://stackoverflow.com/questions/7185589/how-to-calculate-expression-in-java – user2336315 Jan 05 '14 at 11:16
  • I have added jav8-jsr223-win-amd64-0.6.jar in lib and i am able to use these statements in my app but the app crashes when i execute.eval().. is there any solution to it? – user2475511 Jan 05 '14 at 11:21
  • Well I have no experience with this library, but posting the relevant android code and the stacktrace would be helpful to solve the problem. – user2336315 Jan 05 '14 at 11:25
  • ScriptEngineManager factory = new ScriptEngineManager(); final ScriptEngine engine = factory.getEngineByName("Jav8"); String text2=filter(userInputText.getText().toString()); try { userInputText.setText((engine.eval(text2)).toString()); } catch (ScriptException e) { // TODO Auto-generated catch block e.printStackTrace(); } – user2475511 Jan 05 '14 at 11:31
  • Post it in the question by editing it, and please use a code block. – ADTC Jan 05 '14 at 11:40

1 Answers1

0

Have you tried to cast it? like this,

double a = (double) engine.eval("25+36");

or this (toString());

System.out.println(new ScriptEngineManager().getEngineByName("JavaScript").eval(string).toString());
Sabri Meviş
  • 2,231
  • 1
  • 32
  • 38