我正在尝试从 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
}
最佳答案
您在 Main
类中有一个名为 mapAllBreathTest
的 Map
。然后,在您的 BreathTestExtractorMethods
类中,您有另一个名为 mapAllBreathTest
的 Map
对象。 NameExtractor
负责填充 mapAllBreathTest
。而且它从来没有被调用过。
因此,在您的 main
中,您应该调用 BreathTestExtractorMethods
的 NameExtractor
方法:
String str = //Extracted text file ;
String HospNum="1234"
BreathTestExtractorMethods BT =new BreathTestExtractorMethods(HospNum);
mapAllBreathTest = BT.NameExtractor("RIGHT STRING TO PASS");
System.out.println(mapAllBreathTest);
祝你好运。
关于java - 无法访问java中另一个类中方法的返回实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37109381/