使用 Buffered Reader 从文本文件制作 HashMap?
Making a HashMap from a text file using Buffered Reader?
我在编写一个方法时遇到问题,该方法给定一个参数,一个文件名,读取包含单词的文件并制作一个以键为首字母的哈希图,例如对于文件中的单词,例如 apple ,'a' 是键,值是 'apple'。
我目前拥有的代码:
public class WordStore {
HashMap<String, List<String>> map;
File filename;
public WordStore() {
map = new HashMap<String, List<String>>();
}
public WordStore(File file) throws IOException {
map = new HashMap<String, List<String>>();
BufferedReader br = null;
//k = "/Users/hon/eclipse-workspace/Assignment4/src/wordbank.txt"
try{
File filename = new File(k);
FileReader fr = new FileReader(filename);
br = new BufferedReader(fr);
while(br.readLine()!=" ") {
String word ="";
word = br.readLine();
String key = ""+(word.charAt(0));
map.put(key, word);
}
}
catch(IOException e) {
System.out.println("File not found exception caught!");
}
finally {
if(br != null) {
try {
br.close();
}
catch(IOException e) {
e.printStackTrace();
}
}
}
}
public void put(String key, String word) {
if(map.containsKey(key)) {
(map.get(key)).add(word);
}
else {
List<String> names = new ArrayList<>();
names.add(word);
map.put(key, names);
}
}
}
我在 map.put(key, word) 处的构造函数 WordStore(File file) 中有一个错误,它表示类型 HashMap<String, List<String>>
中的方法 put(String, List<String>)
不适用于参数(字符串,字符串)。
我尝试重命名我的 put 方法,以便它使用我的方法而不是 hashmap put 方法,但这也不起作用。
您正试图将 String
值放在需要 List
的位置。这是因为您调用了 map
的常规 put
方法。改为调用您自己的 put
方法,该方法已经处理列表。
put(key, word);
另外,您读取文件的方式不对。首先,您将字符串与 !=
进行比较,这是不正确的。然后,您在每个循环中调用 readLine
两次。循环的顶部应该是:
String word;
while( (word = br.readLine() ) != null) {
这会读取一行并将其与同一行中的 null
进行比较以测试 end-of-file。
您创建了 put
方法,这没问题,但您没有调用它。
相反,您使用 "original" put
方法 map
.
因此将 map.put
的第一个实例更改为 put
。
我在编写一个方法时遇到问题,该方法给定一个参数,一个文件名,读取包含单词的文件并制作一个以键为首字母的哈希图,例如对于文件中的单词,例如 apple ,'a' 是键,值是 'apple'。
我目前拥有的代码:
public class WordStore {
HashMap<String, List<String>> map;
File filename;
public WordStore() {
map = new HashMap<String, List<String>>();
}
public WordStore(File file) throws IOException {
map = new HashMap<String, List<String>>();
BufferedReader br = null;
//k = "/Users/hon/eclipse-workspace/Assignment4/src/wordbank.txt"
try{
File filename = new File(k);
FileReader fr = new FileReader(filename);
br = new BufferedReader(fr);
while(br.readLine()!=" ") {
String word ="";
word = br.readLine();
String key = ""+(word.charAt(0));
map.put(key, word);
}
}
catch(IOException e) {
System.out.println("File not found exception caught!");
}
finally {
if(br != null) {
try {
br.close();
}
catch(IOException e) {
e.printStackTrace();
}
}
}
}
public void put(String key, String word) {
if(map.containsKey(key)) {
(map.get(key)).add(word);
}
else {
List<String> names = new ArrayList<>();
names.add(word);
map.put(key, names);
}
}
}
我在 map.put(key, word) 处的构造函数 WordStore(File file) 中有一个错误,它表示类型 HashMap<String, List<String>>
中的方法 put(String, List<String>)
不适用于参数(字符串,字符串)。
我尝试重命名我的 put 方法,以便它使用我的方法而不是 hashmap put 方法,但这也不起作用。
您正试图将 String
值放在需要 List
的位置。这是因为您调用了 map
的常规 put
方法。改为调用您自己的 put
方法,该方法已经处理列表。
put(key, word);
另外,您读取文件的方式不对。首先,您将字符串与 !=
进行比较,这是不正确的。然后,您在每个循环中调用 readLine
两次。循环的顶部应该是:
String word;
while( (word = br.readLine() ) != null) {
这会读取一行并将其与同一行中的 null
进行比较以测试 end-of-file。
您创建了 put
方法,这没问题,但您没有调用它。
相反,您使用 "original" put
方法 map
.
因此将 map.put
的第一个实例更改为 put
。