Below is my code.
I am not sure why am I getting the error
"javax.script.ScriptException: sun.org.mozilla.javascript.internal.EcmaError: ReferenceError: "lcc" is not defined......."

import java.io.BufferedReader;
import java.io.InputStreamReader;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

public class Eval {

        public static void main (String[] args) throws Exception {
	ScriptEngineManager manager = new ScriptEngineManager();
	ScriptEngine engine = manager.getEngineByName("JavaScript");
			         
	System.out.println("Please enter the two numbers separated by commas and press Enter");
	BufferedReader consoleinput = new BufferedReader(new InputStreamReader(System.in));
	String input = consoleinput.readLine();
	String[] lc = input.split(",");
			  		
	int lclen = lc.length;
					
	for (int i=0;i<lclen;i++)
		{
		String xx = "lcc["+i+"]";
		engine.put(xx, lc[i]);
		}
			  		
		 try 
		 {	   
		 String script = "lcc[0]>lcc[1]";
		Object bol = engine.eval(script);
		System.out.println(bol);
		}
			        
		 catch (ScriptException e) 
		{
		 e.printStackTrace();
		}
}
}

Recommended Answers

All 5 Replies

Put an array into dictionary.

...
 engine.put("lcc",lc);			
 String script ="lcc[0]>lcc[1]";
 Object bol = engine.eval(script);
 System.out.println(bol);
 ...

Thanks a lot adatapost. That worked perfectly :icon_smile:

Could I use the same concept of dictionary for a string match with regex

E.g.
String str = "N";
if (str.matches[lc])
{
// do something
}

which means which ever character(s) the user inputs, the program should see if String str matches any of those characters.

Any ideas to my second query?

>Any ideas to my second query?

No. Sorry, I'm not quite following the problem.

I was hoping something like this would work

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class test {

        public static void main (String[] args) throws Exception {
	ScriptEngineManager manager = new ScriptEngineManager();
	ScriptEngine engine = manager.getEngineByName("JavaScript");
			         
	System.out.println("Please enter the two numbers separated by commas and press Enter");
	BufferedReader consoleinput = new BufferedReader(new InputStreamReader(System.in));
	String input = consoleinput.readLine();
	String[] lc = input.split(",");
        
        String str = "N";
	  		
		  if (str.equals(lc))
                      {
                           System.out.println("Match found");
                      }
}
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.