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();
		}
}
}

Edited 6 Years Ago by ontherocks: n/a

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.

Edited 6 Years Ago by ontherocks: n/a

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");
                      }
}
}
This article has been dead for over six months. Start a new discussion instead.