beanshell newbie-运行简单脚本时出错

时间:2012-09-01 20:50:46

标签: java beanshell

我正在尝试在Beanshell中运行一个简单的switch-case语句

这是我试图运行的代码 -

temp = assignee.toString();
switch( temp.toString() ) 
 {
case 'missing' : check = "missing"; break;
case '404' : check = "404"; break;
default: check = "data"; break;
}

但是我收到以下错误 -

 ERROR - Error during script execution: Sourced file: inline evaluation of: ``temp = assignee.toString(); switch( temp.toString() ) { case 'missing' : check = . . . '' Token Parsing Error: Lexical error at line 3, column 8.  Encountered: "i" (105), after : "\'m"
 org.webharvest.exception.ScriptException: Error during script execution: Sourced file: inline evaluation of: ``temp = assignee.toString(); switch( temp.toString() ) { case 'missing' : check = . . . '' Token Parsing Error: Lexical error at line 3, column 8.  
Encountered: "i" (105), after : "\'m"
at org.webharvest.runtime.scripting.BeanShellScriptEngine.eval(Unknown Source)

我在这里做错了什么?如何解决此错误?

1 个答案:

答案 0 :(得分:2)

BeanShell中的字符串文字与Java一样,必须使用双引号,而不是单引号:

bsh % x = 'missing';
// Error: Error parsing input: bsh.TokenMgrError: Lexical error at line 1, column 37.  Encountered: "i" (105), after : "\'m"
bsh % x = "missing";
bsh % print(x);
missing
bsh % 

单引号用于字符文字。对多字符字符串使用单引号会给出错误,例如Encountered: "i" (105), after : "\'m",这是因为BeanShell期待'之后的另一个m(结束字符文字),但它改为i

相关问题