任务是:第一个文件是随机单词,第二个文件是字母和值。 我必须将这两个文件读入地图数据结构,而对于另一个文件中的每个单词,我必须打印出它们的点值:
`在这里输入代码 ``
static void readMap(String path, HashMap<String,Object> set) {
`try {
BufferedReader in = new BufferedReader(new FileReader(path));
while(in.readLine() != null) {
String line = in.readLine();
set.put(line,1);
}
in.close();
} catch(FileNotFoundException x) {
System.out.println(x.getMessage());
} catch(IOException y) {
System.out.println(y.getMessage());
}
}
这就是我所拥有的。但我不知道如何比较这两个文件...... 谢谢你的帮助!
答案 0 :(得分:0)
您需要做的两件事:
我提供了一些代码,但您需要自己实现一些代码。
`
HashMap<Character, Integer> map = new HashMap<Character, Integer>();
BufferedReader in = new BufferedReader(new FileReader("letters.txt"));
while(in.readLine() != null) {
String line = in.readLine();
//TODO you need to split the string by ';' and get the char and int
char c = ...
int x = ...
set.put(c,x);
}
BufferedReader in = new BufferedReader(new FileReader("random_words.txt"));
while(in.readLine() != null) {
String line = in.readLine();
int total = 0;
for(int i = 0; i < line.length(); i++)
{
char c = line.charAt(i);
//TODO get integer value from map, add it to the total, and print it after the loop
int value = ...
total += value;
}
System.out.println("value for word " + line + " is " + total);
}