2

I'll make my question to the point. I have a string with some value and operators. Let's take an example of

4 * 24 + Math.sqrt(36) - 5

So if I do Integer.parseInt() it crashed. On further search I found out that I have to use a parser. But they were for command line java whereas mine is in android.

So my question is how do I convert this string "4 * 24 + Math.sqrt(36) - 5" into an integer which calculates the result? Also will this method include the Math.sqrt () function?

Thanks.

EDIT 1:

In response to hacker13ua

When I try your MAVEN method I get this error. Any Idea Why?

Caused by: java.lang.StringIndexOutOfBoundsException: length=1; regionStart=0; regionLength=3

My Code:

final String foo = "4 * 24 + Math.sqrt(36) - 5";
                    Object i = MVEL.eval(foo);
                    String in = i.toString();
                    Toast.makeText(MainActivity.this,String.valueOf(in).toString(), Toast.LENGTH_LONG).show();

And this is in my gradle build file

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.2.0'
compile 'org.mvel:mvel2:2.2.7.Final'

}

Thanks

KISHORE_ZE
  • 1,466
  • 3
  • 16
  • 26
  • please check this link... http://stackoverflow.com/questions/8661634/execute-java-code-in-java Hope it helps you – Anas Na Mar 21 '16 at 09:52
  • You could try and see if you get any of the common expression languages to run on Android; start with [Unified EL](https://en.wikipedia.org/wiki/Unified_Expression_Language) for instance. – JimmyB Mar 21 '16 at 09:52

1 Answers1

2

You can use javascript engine.

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

public class Main
{
    public static void main(final String[] args) throws ScriptException
    {
        final ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
        final ScriptEngine engine = scriptEngineManager.getEngineByName("javascript");
        final String foo = "4 * 24 + Math.sqrt(36) - 5";
        System.out.println(engine.eval(foo));
    }
}

Or you can use MVEL. Maven dependency:

<dependency>
    <groupId>org.mvel</groupId>
    <artifactId>mvel2</artifactId>
    <version>2.2.7.Final</version>
</dependency>

Your code:

import org.mvel2.MVEL;

public class Main
{
    public static void main(final String[] args)
    {
        final String foo = "4 * 24 + Math.sqrt(36) - 5";
        System.out.println(MVEL.eval(foo));
    }
}
Yevhen Surovskyi
  • 931
  • 11
  • 19
  • System.println Is command line java. However for the maven one 1 need my pc. I'm on travel and thus am using an ide on phone. Give me a couple of days and I'll reply to the maven answer. However the first part I believe is meant for command line. I saw this answer to another question and I couldn't import the classes. That's why – KISHORE_ZE Mar 21 '16 at 10:12
  • Please see my edits above. Also how do you use the javax method. Do I have to add some API. If so how? – KISHORE_ZE Mar 22 '16 at 19:02