2012-01-18 11 views
0

私はこれで間違っているのだろうかと疑問に思っています:現在、StringDirectiveクラスをテストしています。これは作成するString変数の名前の入力文字列を解析することになっています。私はTPLStringクラスを正しく設定したと思っていましたが、シンボルエラーを複数の行で見つけることができませんでした - 私が間違って渡したパラメータですか?このコードは、文字列を解析し、2つに分割し、文字列変数名を解析し、空の文字列を今の値として代入し、変数の名前と値に関する情報を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

あなたが具体的にどのようなエラーが発生しているに投稿するSSCCE原則に従うことができます示唆名

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

だ使用する必要がありますが?あなたは必要なファイルを含んでいますか? – cdeszaq

+0

ええ、私が持っているのは、含まれていない唯一のコードは、Stringを[keyword + values]の形式に解析するための元のメソッドです。これはDirectiveクラスにマップされた最初のHashMap用です。上記のStringDirectiveクラス。 – Luinithil

+0

@cdeszaq大きな問題となる最初のエラーは、 'variables.put(name、TPLString(name、value));行のTPLStringを見つけることができないと言うエラーです。 – Luinithil

答えて

0

TPLString(name, value)は正しい構文ではありません。

新しいTPLVariableが必要な場合は、その前に新しいキーワードを追加する必要があります。

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

あなたが宣言以前の変数を参照したい場合、あなたはそれは私はあなたが質問次回

関連する問題