我正在尝试从Java中的另一个类访问填充的HashMap。由于某种原因,我不能。我认为该代码是正确的,但显然我遗漏了一些东西。hashMap可以很好地填充,但是我无法访问它。
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
}
您在班上有一个Map
电话。然后,在您的班级中,您还有另一个对象称为。该负责填充。而且它从未被调用过。mapAllBreathTest
Main
BreathTestExtractorMethods
Map
mapAllBreathTest
NameExtractor
mapAllBreathTest
因此,您main
应该调用以下NameExtractor
方法BreathTestExtractorMethods
:
String str = //Extracted text file ;
String HospNum="1234"
BreathTestExtractorMethods BT =new BreathTestExtractorMethods(HospNum);
mapAllBreathTest = BT.NameExtractor("RIGHT STRING TO PASS");
System.out.println(mapAllBreathTest);
祝你好运。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句