即使定义了类和参数,Java也找不到符号错误?
我想知道我在这方面做错了什么:目前正在测试即使定义了类和参数,Java也找不到符号错误?,java,compiler-errors,hashmap,parameter-passing,Java,Compiler Errors,Hashmap,Parameter Passing,我想知道我在这方面做错了什么:目前正在测试StringDirective类,该类应该解析输入字符串以获得要创建的字符串变量的名称。我在想我已经正确地设置了TPLString类,但是在多行上出现了一大堆找不到符号的错误——我传入的参数是错误的吗?这段代码应该解析一个字符串,将其拆分为两部分,解析为一个字符串变量名,然后为其分配一个空字符串作为当前值,然后将有关变量名和值的信息存储在HashMap中 public class StringStatement implements Directive
StringDirective
类,该类应该解析输入字符串以获得要创建的字符串变量的名称。我在想我已经正确地设置了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
}
}
类TPLVariables
{
私有映射变量=新HashMap();
public void addVariable(字符串名称、字符串值)
{
//解析声明字符串,创建适当类型的TPLVariable
//并使用变量名作为键将其添加到映射中
if(value.charAt(0)='“')
{
TPLString stringDeclaration=新的TPLString(名称、值);
变量.put(名称,TPLString(名称,值));
System.out.println(name+“hex0”);//调试
System.out.println(值+“hex1”);//调试
}
其他的
{
TPLInt integerdeparation=新的TPLInt(名称、值);
变量.put(名称,TPLInt(名称,值));
System.out.println(name+“hex2”);//调试
System.out.println(值+“hex3”);//调试
}
}
TPLString(名称、值)
语法不正确
如果您想要一个新的TPLVariable,您应该在它之前添加新的关键字
variables.put(name, new TPLString(name, value));
若要引用声明的前一个变量,应使用其名称
TPLString stringDeclaration= new TPLString(name, value);
variables.put(name, stringDeclaration);
我建议您下次可以遵循原则发布问题您会遇到什么具体错误?您是否包含所需的文件?是的,我想我已经包含了--唯一没有包含的代码是将字符串解析为格式[关键字+值]的原始方法;这是映射到指令类的第一个HashMap,例如上面的StringDirective类。@cdeszaq第一个令人担忧的错误是,它说在
variables.put(name,TPLString(name,value))行中找不到TPLString;
我看不到任何包或导入声明——您的所有类都在默认包中吗?@Tom G Yes。由于导入声明很好,所以停止了导入声明:导入声明是import.java.io.*;
import java.util.*;