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

发布于 2024-12-27 16:54:51 字数 2216 浏览 0 评论 0原文

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


    } 

I'm wondering what I'm doing wrong with this: Currently testing the StringDirective class, which is supposed to parse the input string for the name of the String variable to be created. I was thinking I've set the TPLString class up correctly but get a whole boatload of cannot find symbol errors on multiple lines-- are the parameters I passed in wrong? This code is supposed to parse a String, split it in two parts, parse it for a String variable name, and then assign it an empty string as a value for now, and then store the information about the variable's name and value in a 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
    }
}

//variable classes

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;
    }

}

//add to variables 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
        }


    } 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

執念 2025-01-03 16:54:52

TPLString(name, value) 不是正确的语法。

如果你想要一个新的 TPLVariable,你应该在它前面添加 new 关键字。

variables.put(name, new TPLString(name, value));

如果你想引用之前声明的变量,你应该使用它的名称

TPLString stringDeclaration= new TPLString(name, value);
variables.put(name, stringDeclaration);

我建议你可以遵循 SSCCE 原则来发布问题时间

TPLString(name, value) is not a correct syntax.

If you want a new TPLVariable, you should add new keyword before it.

variables.put(name, new TPLString(name, value));

If you want to reference previous variable you declare, you should use it's name

TPLString stringDeclaration= new TPLString(name, value);
variables.put(name, stringDeclaration);

I suggest you can follow SSCCE principle to post question next time

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文