5

The following code do the calculation for data in String rani=String rani = "32*0.25"; and gives the correct output as 8.0

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

public class StringMathEngine {

    public static void main(String[] args) throws ScriptException {

        ScriptEngineManager mgr = new ScriptEngineManager();
        ScriptEngine engine = mgr.getEngineByName("JavaScript"); 
        String rani =  "32*0.25";
        System.out.println(engine.eval(rani));
    }
}

But fails when it is employed in the following code:

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

public class StringMathEngine {

    public static void main(String[] args) throws ScriptException {

        ScriptEngineManager mgr = new ScriptEngineManager();
        ScriptEngine engine = mgr.getEngineByName("JavaScript");



        String[]rani = {"s", "32*0.25", "r", "32*0.75+16", "r", "16", "s", "32"};

        for(int n=0;n<rani.length; n++){
            if(rani[n].equals("s")) {
                rani[n]=rani[n].replaceAll("s","C/");
            }

            else if(rani[n].equals("r")){
                 rani[n]=rani[n].replaceAll("r","D/");
            } 
            else {
                 rani[n]=engine.eval(rani[n]);
            } 

            System.out.println(rani[n]);
        }
    }
}

Being new to programming I need help to correct this code.

2
  • 1
    Could you elaborate what "fails" means? Exception, wrong result, ?... Commented Jun 18, 2014 at 13:22
  • Need more info. from poster. If compile time error what is the error? Is it just that missing semi-colon Joop Eggen pointed out? Commented Jun 18, 2014 at 13:45

1 Answer 1

4

(Initially there was a missing semicolon.)

The problem was that ScriptEngine.eval(...) returns an Object, and there is a System.out.println(Object). However assigning the object to a String gives an appropiate error.

for (int n=0; n < rani.length; n++) {
    if (rani[n].equals("s")) {
        rani[n] = rani[n].replace("s","C/");
    } else if(rani[n].equals("r")) {     // ('else' missing)
        rani[n] = rani[n].replace("r","D/"); 
    } else {
        rani[n] = engine.eval(rani[n]);  // Semicolon missing!
        try {
            rani[n] = String.valueOf(engine.eval(rani[n]));
        } catch (ScriptException e) {
            e.printStackTrace(System.out);
        }
    } 
    System.out.println(rani[n]);
}

Alternatively you might use variables:

engine.put("s", "C/");
engine.put("r", "D/");
Sign up to request clarification or add additional context in comments.

6 Comments

Even after correcting the missing parts the code is not working. Kindly handle it for further correction.
Fine, now the error should be clear. You got something as String expected where Object was encountered.
If I rectify this as: rani[n]=(String) engine.eval(rani[n]); This runtime error comes: Exception in thread "main" java.lang.ClassCastException: java.lang.Double cannot be cast to java.lang.String
Yes the expression evaluates to a double/Double as it is numeric. Awesome isn't it? Hence the String.valueOf(...). You could also do "" + eval(...)
Hi Joop Eggen, you have found an embarrassing mistake, kindly complete the syntax: String.valueOf(...). Being new to programming it will be a turning point for me. Kindly complete the code.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.