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.

Soon
  • 339
  • 2
  • 9
  • 1
    Could you elaborate what "fails" means? Exception, wrong result, ?... – Icewind Jun 18 '14 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? – Andrew_CS Jun 18 '14 at 13:45

1 Answers1

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/");
Joop Eggen
  • 96,344
  • 7
  • 73
  • 121
  • Even after correcting the missing parts the code is not working. Kindly handle it for further correction. – Soon Jun 18 '14 at 14:04
  • Fine, now the error should be clear. You got something as String expected where Object was encountered. – Joop Eggen Jun 18 '14 at 14:26
  • 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 – Soon Jun 18 '14 at 14:41
  • 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(...)` – Joop Eggen Jun 18 '14 at 14:51
  • 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. – Soon Jun 18 '14 at 15:11
  • The code worked as needed. Oh! Great man Joop Eggen, I am happy that you have solved one of the rarest problems. – Soon Jun 18 '14 at 15:27