使用 ScriptEnging 评估 java 中的字符串
Evaluating String in java Using ScriptEnging
下面是我的代码,用于获取一些简单的用户输入 hello 或 mouse 并搜索字符串(稍后我将在更大的文本文件上使用它)。
我用它创建了 Java 行代码,该代码行使用了 contains 方法。之后,我需要对其进行评估。我在使用脚本引擎时找到了一些帮助,但我一直收到此错误:Exception in thread "main" javax.script.ScriptException: ReferenceError: "name" is not defined in <eval> at line number 1
来自我要评估的代码的输出是 name.contains("hello") || name.contains("mouse")
.
任何帮助都会很棒。谢谢
public static void main(String args[]) throws ScriptException {
String searchString = "name";
String terms = "(hello) || (mouse)";
String command = "";
//multiple commands
if(terms.contains("(") && terms.contains(")") && terms.contains("[") && terms.contains("]")){
command = terms.replace(")","\")");
command=command.replace("(", searchString + ".contains(\"");
command = command.replace("[","(");
command = command.replace("]",")");
}
//only ands/ only ors
else {
command = terms.replace("(", searchString + ".contains(\"");
command = command.replace(")", "\")");
}
System.out.println(command);
String name = "mousehellonamepe";
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("js");
Object result = engine.eval(command);
System.out.println(result);
你到底想做什么?
=======更新======
首先,String terms 应该是一个列表。
成功:
ArrayList<String> searchTerms = new ArrayList();
按如下方式在该列表中添加任意数量的单词:
// using add() to initialize values
arr.add("Hello");
arr.add("Mouse");
arr.add("Bike");
arr.add("House");
// use contains() to check if the element
// 2 exits or not
boolean mouseExists = arr.contains("Mouse");
if (ans)
System.out.println("Our list contains the word mouse");
else
System.out.println("Our list does not contain the word mouse");
这应该输出:
Our list contains the word mouse
代码应如下所示:
public static void main(String args[]) throws ScriptException {
ArrayList<String> searchTerms = new ArrayList();
arr.add("Hello");
arr.add("Mouse");
arr.add("Bike");
arr.add("House");
boolean mouseExists = arr.contains("Mouse");
if (ans)
System.out.println("Our list contains the word mouse");
else
System.out.println("Our list does not contain the word mouse");
}
Interpreter i = new Interpreter();
i.set("searchThis", searchString);
i.set("answer", i.eval(command));
Boolean answer2 = (Boolean) (i.get("answer"));
http://www.beanshell.org/manual/quickstart.html#Calling_BeanShell_From_Your_Application
我用它来评估命令。
上面的link提供了所有的解释!
下面是我的代码,用于获取一些简单的用户输入 hello 或 mouse 并搜索字符串(稍后我将在更大的文本文件上使用它)。
我用它创建了 Java 行代码,该代码行使用了 contains 方法。之后,我需要对其进行评估。我在使用脚本引擎时找到了一些帮助,但我一直收到此错误:Exception in thread "main" javax.script.ScriptException: ReferenceError: "name" is not defined in <eval> at line number 1
来自我要评估的代码的输出是 name.contains("hello") || name.contains("mouse")
.
任何帮助都会很棒。谢谢
public static void main(String args[]) throws ScriptException {
String searchString = "name";
String terms = "(hello) || (mouse)";
String command = "";
//multiple commands
if(terms.contains("(") && terms.contains(")") && terms.contains("[") && terms.contains("]")){
command = terms.replace(")","\")");
command=command.replace("(", searchString + ".contains(\"");
command = command.replace("[","(");
command = command.replace("]",")");
}
//only ands/ only ors
else {
command = terms.replace("(", searchString + ".contains(\"");
command = command.replace(")", "\")");
}
System.out.println(command);
String name = "mousehellonamepe";
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("js");
Object result = engine.eval(command);
System.out.println(result);
你到底想做什么?
=======更新====== 首先,String terms 应该是一个列表。
成功:
ArrayList<String> searchTerms = new ArrayList();
按如下方式在该列表中添加任意数量的单词:
// using add() to initialize values
arr.add("Hello");
arr.add("Mouse");
arr.add("Bike");
arr.add("House");
// use contains() to check if the element
// 2 exits or not
boolean mouseExists = arr.contains("Mouse");
if (ans)
System.out.println("Our list contains the word mouse");
else
System.out.println("Our list does not contain the word mouse");
这应该输出:
Our list contains the word mouse
代码应如下所示:
public static void main(String args[]) throws ScriptException {
ArrayList<String> searchTerms = new ArrayList();
arr.add("Hello");
arr.add("Mouse");
arr.add("Bike");
arr.add("House");
boolean mouseExists = arr.contains("Mouse");
if (ans)
System.out.println("Our list contains the word mouse");
else
System.out.println("Our list does not contain the word mouse");
}
Interpreter i = new Interpreter();
i.set("searchThis", searchString);
i.set("answer", i.eval(command));
Boolean answer2 = (Boolean) (i.get("answer"));
http://www.beanshell.org/manual/quickstart.html#Calling_BeanShell_From_Your_Application
我用它来评估命令。 上面的link提供了所有的解释!