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

标签 java compiler-errors hashmap parameter-passing

我想知道我在做什么错:当前正在测试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原则下一次发布问题

关于java - 即使定义了类和参数,Java也找不到符号错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8913849/

相关文章:

compiler-errors - 在Android Studio中编译Cocos 2d-x项目时发生错误

compiler-errors - Ada95 Tagged Limited Record 给我错误 "Completion of nonlimited type cannot be limited"

javascript - 如何使用 Node.JS 在哈希表中存储每个键的多个值?

java - ScenicView 8.6.0 未找到 JavaFX 应用程序

java - 如何使用服务到服务身份验证来使用 Java 进行 Azure Key Vault 的本地开发?

java - Java 中的多个类和主要方法,以及包、 namespace

java - java中如何决定使用哪个通配符?

JAVA BigInteger类错误: BigInteger: modulus not positive

c++ - 键为 2 的幂的快速映射 (C++)

java - 如何从文本文档创建 HashMap 并显示特定的键和值