即使定义了类和参数,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
}
}
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
TPLString(name, value)
不是正确的语法。如果你想要一个新的 TPLVariable,你应该在它前面添加 new 关键字。
如果你想引用之前声明的变量,你应该使用它的名称
我建议你可以遵循 SSCCE 原则来发布问题时间
TPLString(name, value)
is not a correct syntax.If you want a new TPLVariable, you should add new keyword before it.
If you want to reference previous variable you declare, you should use it's name
I suggest you can follow SSCCE principle to post question next time