java - 即使定义了类和参数,Java也找不到符号错误?

我想知道我在做什么错:当前正在测试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;
    }

}

//添加到变量HashMap
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);

我建议您可以遵循SSCCE原则下一次发布问题

https://stackoverflow.com/questions/8913849/

相关文章:

c++ - `invalid initialization of non-const referen

java - 保存一个整数onClick,以便以后可以在android应用中调用它

java - 错误包org.python.util不存在,使用ant进行编译

java - Apache Wink请求处理程序

java - Eclipse 拒绝我的公共(public) void init 方法

C++ - 清理后 Unresolved external symbol 错误

flash - Flash编译器不允许覆盖

ios - 导出版本时也出错,找不到iOS arm-apple-darwin9-as.exe

compiler-errors - Play Framework [2.0]-更改模型破坏了我的见解

c++ - 算法::binary_search call中的预期主要表达式