【发布时间】:2016-05-18 06:13:40
【问题描述】:
使用来自 link 的代码将文本文件内容加载到 GUI:
Map<String, String> sections = new HashMap<>();
Map<String, String> sections2 = new HashMap<>();
String s = "", lastKey="";
try (BufferedReader br = new BufferedReader(new FileReader("input.txt"))) {
while ((s = br.readLine()) != null) {
String k = s.substring(0, 10).trim();
String v = s.substring(10, s.length() - 50).trim();
if (k.equals(""))
k = lastKey;
if(sections.containsKey(k))
v = sections.get(k) + v;
sections.put(k,v);
lastKey = k;
}
} catch (IOException e) {
}
System.out.println(sections.get("AUTHOR"));
System.out.println(sections2.get("TITLE"));
如果是input.txt的if内容:
AUTHOR authors name
authors name
authors name
authors name
TITLE Sound, mobility and landscapes of exhibition: radio-guided
tours at the Science Museum
现在我想计算 HashMap 中的值,但是 sections.size() 计算存储在文本文件中的所有数据行。
我想问一下如何计算项目,即sections 中的值v?如何根据作者姓名获得编号4?
【问题讨论】: