【问题标题】:Java cannot find symbol errors even though class and parameters has been defined?即使已经定义了类和参数,Java 也找不到符号错误?
【发布时间】:2012-01-18 16:39:23
【问题描述】:

我想知道我做错了什么:目前正在测试StringDirective 类,它应该解析输入字符串以获取要创建的字符串变量的名称。我在想我已经正确设置了TPLString 类,但是在多行上出现了一大堆找不到符号错误——我传入的参数是否错误?这段代码应该解析一个String,把它分成两部分,解析一个String变量名,然后给它分配一个空字符串作为现在的值,然后将变量名和值的信息存储在一个@中987654323@。

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
        }


    } 

【问题讨论】:

  • 您遇到了哪些具体错误?您是否包含所需的文件?
  • 是的,我想我有——唯一没有包含的代码是将字符串解析为格式 [keyword+ values] 的原始方法;这是第一个映射到 Directive 类的 HashMap,例如上面的 StringDirective 类。
  • @cdeszaq 第一个值得关注的错误是它说在variables.put(name,TPLString(name, value)); 行中找不到 TPLString
  • 我没有看到包或导入声明——你的所有类都在默认包中吗?
  • @Tom G 是的。省略导入声明,因为它们很好:导入声明是 import.java.io.*; import java.util.*;

标签: java compiler-errors hashmap parameter-passing


【解决方案1】:

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

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

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

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

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

建议你下次可以按照SSCCE的原则发帖

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-17
    • 1970-01-01
    • 2019-10-02
    • 1970-01-01
    • 1970-01-01
    • 2013-10-20
    • 1970-01-01
    • 2020-10-31
    相关资源
    最近更新 更多