2016-05-09 4 views
0

javaの別のクラスから移入されたHashMapにアクセスしようとしています。何らかの理由で私はできません。私はコードが正しいと思うが、明らかに私は何かが欠けていると思う。 hashMapはうまくいきますが、アクセスできません。Javaの別のクラスにある返されたメソッドのインスタンスにアクセスできません

public class Main { 
    public static String HospNum; 
    public static Map<String,String> mapAllBreathTest= new LinkedHashMap<String,String>(); 

     public static void main(String[] args) throws IOException, TikaException, SQLException { 

       String str = //Extracted text file ; 
       String HospNum="1234" 
       BreathTestExtractorMethods BT =new BreathTestExtractorMethods(HospNum); 
       System.out.println(mapAllBreathTest); //EMPTY HASHMAP RETURNED 
       } 
     } 

と呼ばれているクラス:

public class BreathTestExtractorMethods { 
    public String HospNum; 
    public Map<String,String> mapAllBreathTest= new LinkedHashMap<String,String>(); 


     public BreathTestExtractorMethods(String HospNum) { 
      BreathTestExtractorMethods.HospNum=HospNum; 
     } 

     public Map<String,String> NameExtractor(String str){ 
      Pattern match_pattern = Pattern.compile("Patient Name(.*)Date",Pattern.DOTALL); 
      Matcher matchermatch_pattern = match_pattern.matcher(str); 
      if (matchermatch_pattern.find()) { 
       String[] PtName=matchermatch_pattern.group(1).toString().trim().split("\\s"); 
       mapAllBreathTest.put("Sname",PtName[0].trim()); 
       mapAllBreathTest.put("Fname",PtName[1].trim()); 


      } 
      return mapAllBreathTest; //THIS IS A POPULATED HASHMAP 
    } 
+0

あなたはメソッドNameExtractor()を呼び出していません! – jcool

答えて

1

を追加し、それを移入してみてくださいMapMain clにmapAllBreathTestと呼ばれるお尻。次に、BreathTestExtractorMethodsクラスにはmapAllBreathTestという別のMapオブジェクトがあります。 NameExtractorは、mapAllBreathTestを入力します。それは決して呼び出されません。

String str = //Extracted text file ; 
String HospNum="1234" 
BreathTestExtractorMethods BT =new BreathTestExtractorMethods(HospNum); 
mapAllBreathTest = BT.NameExtractor("RIGHT STRING TO PASS"); 
System.out.println(mapAllBreathTest); 

幸運:あなたのmainあなたがBreathTestExtractorMethodsNameExtractorメソッドを呼び出す必要がでそう

+0

Aha。はい、自己へのメモ。実際にメソッドを呼び出すことを忘れないでください.... –

+0

それは助けて喜んで。 – STaefi

0

二つのクラスが異なるパッケージに配置されている場合、あなたはできません。 HashMap変数の前に識別子publicを宣言していません。 public識別子を省略すると、同じクラスとパッケージ内でのみ変数を使用することができ、これらの2つの外部での使用を禁止します。第二の問題を読んで


あなたはまったく同じ名前を持つ2つのHashMap変数を宣言していることです。 2番目のクラスは自身のHashMapを構成します(クラスは最初に独自のスコープを参照するため)。最初のクラスでは空で構成されていない最初にHashMapが印刷されます。このような名前を区別してみてください。

ファーストクラス

public class Main { 
    public static String HospNum; 
    public static Map<String,String> mapAllBreathTest = new LinkedHashMap<>(); //You can just use an empty diamond operator here 

    public static void main(String[] args) throws IOException, TikaException, SQLException { 

     String str = //Extracted text file ; 
     String HospNum ="1234" 
     BreathTestExtractorMethods BT = new BreathTestExtractorMethods(HospNum); 
     System.out.println(this.mapAllBreathTest); 
    } 
} 

セカンドクラス

public class BreathTestExtractorMethods { 
    public String HospNum; 
    public Map<String,String> mapAllBreathTest= new LinkedHashMap<String,String>(); 

    public BreathTestExtractorMethods(String HospNum) { 
     BreathTestExtractorMethods.HospNum=HospNum; 
    } 

    public void NameExtractor(String str){ 
     Pattern match_pattern = Pattern.compile("Patient Name(.*)Date",Pattern.DOTALL); 
     Matcher matchermatch_pattern = match_pattern.matcher(str); 
     if (matchermatch_pattern.find()) { 
      String[] PtName = matchermatch_pattern.group(1).toString().trim().split("\\s"); 
      Main.mapAllBreathTest.put("Sname",PtName[0].trim()); 
      Main.mapAllBreathTest.put("Fname",PtName[1].trim()); 
     } 
     //return mapAllBreathTest; //You don't need a return type as the HashMap is already available in the Main class 
    } 
} 
+0

同じパッケージ内にあります。私は今すべてを公表と宣言しましたが、それでも同じ出力 –

0

あなたはハッシュマップが空になっているが、あなたが持っている

mapAllBreathTest = BT.NameExtractor("stringOfYourLogic") 
関連する問題