我想知道我在做什么错:当前正在测试StringDirective
类,该类应该解析输入字符串以获取要创建的String变量的名称。我以为我已经正确设置了TPLString
类,但是却遇到了很多麻烦,无法在多行上找到符号错误-我传入的参数有误吗?该代码应该解析一个字符串,将其分为两部分,解析为一个字符串变量名称,然后为它分配一个空字符串作为值,然后将有关变量名称和值的信息存储在HashMap
中。
public class StringStatement implements Directive
{ /** StringStatement implements the STRING keyword as defined in class TPLString.
* This keyword declares a String variable.
* A declared String is empty when first instantiated.
*/
public void execute(String[] parts)
{
//instantiate a TPLString
String temp=parts[1];
String[] placeholder = temp.split("[\\s+]");
String name=placeholder[0];
String value;
variables.addVariable(name, value);//add variable to variables hashmap
}
}
abstract class TPLVariable
{
String name;
TPLVariable(String s)
{
name = s;
}
}
class TPLInt extends TPLVariable
{
int intValue;
TPLInt(String s, int v)
{
super(s);
intValue=v;
}
}
class TPLString extends TPLVariable
{
String stringValue;
TPLString(String s, String str)
{
super(s);
stringValue=str;
}
}
class TPLVariables
{
private Map<String, TPLVariables> variables = new HashMap<String, TPLVariables>();
public void addVariable(String name, String value)
{
// Parses the declaration String, create a TPLVariable of the appropriate type
// and add it to the map using the variable name as the key
if(value.charAt(0)=='"')
{
TPLString stringDeclaration= new TPLString(name, value);
variables.put(name, TPLString(name, value));
System.out.println(name+ " hex0");//debug
System.out.println(value+ " hex1");//debug
}
else
{
TPLInt integerDeclaration= new TPLInt(name, value);
variables.put(name, TPLInt(name, value));
System.out.println(name+ " hex2");//debug
System.out.println(value+ " hex3");//debug
}
}
最佳答案
TPLString(name, value)
不是正确的语法。
如果要使用新的TPLVariable,则应在其之前添加新的关键字。
variables.put(name, new TPLString(name, value));
TPLString stringDeclaration= new TPLString(name, value);
variables.put(name, stringDeclaration);
https://stackoverflow.com/questions/8913849/
相关文章:
c++ - `invalid initialization of non-const referen
java - 保存一个整数onClick,以便以后可以在android应用中调用它
java - 错误包org.python.util不存在,使用ant进行编译
java - Eclipse 拒绝我的公共(public) void init 方法
C++ - 清理后 Unresolved external symbol 错误
ios - 导出版本时也出错,找不到iOS arm-apple-darwin9-as.exe