2012-01-18 56 views
0

我想知道我在做什麼錯了:目前正在測試StringDirective類,它應該解析輸入字符串以獲得要創建的字符串變量的名稱。我在想我已經正確地設置了TPLString類,但是得到了一大堆無法在多行上找到符號錯誤 - 我通過的參數是錯誤的嗎?這段代碼應該解析一個字符串,將它分成兩部分,解析爲一個字符串變量名稱,然後爲它分配一個空字符串作爲現在的值,然後將關於變量名稱和值的信息存儲在一個HashMap即使定義了類和參數,Java仍找不到符號錯誤?

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 
     } 


    } 
+2

你得到什麼具體的錯誤?你是否包含所需的文件? – cdeszaq 2012-01-18 16:42:04

+0

是的,我認爲 - 唯一沒有包含的代碼是將字符串解析爲[keyword + values]格式的原始方法;這是第一個映射到指令類的HashMap,例如上面的StringDirective類。 – Luinithil 2012-01-18 17:01:05

+0

@cdeszaq第一個值得關注的錯誤是它說它無法在行'variables.put(name,TPLString(name,value))中找到TPLString';' – Luinithil 2012-01-18 17:07:35

回答

0

TPLString(name, value)是不是一個正確的語法。

如果你想要一個新的TPLVariable,你應該在它之前添加新的關鍵字。

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

如果你想引用你聲明一個變量,你應該使用它的名字

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

我建議你可以按照SSCCE原則問題,下一次發佈