我正在尝试在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)
我在这里做错了什么?如何解决此错误?
最佳答案
像Java中一样,BeanShell中的字符串文字必须使用双引号,而不是单引号:
bsh%x ='丢失';
//错误:解析输入时出错:bsh.TokenMgrError:第1行第37列的词法错误。遇到:“i”(105),在:“\'m”之后
bsh%x =“丢失”;
bsh%打印(x);
失踪
bsh%
单引号用于字符文字。对多字符字符串使用单引号会给您一个错误,例如Encountered: "i" (105), after : "\'m"
,这是因为BeanShell期望在'
之后使用另一个m
(以结束字符文字),但是却改为使用i
。