将2个文件读入地图字符

Read 2 Files into map characters

任务是:第一个文件包含随机单词,第二个文件包含字母表中的字母和值。 我必须将这两个文件读入地图数据结构,对于另一个文件中的每个单词,我必须打印出它们的点值:

`在此处输入代码 ``

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());
            }
        }

这就是我的。但我不知道如何比较这两个文件...... 感谢您的帮助!

您需要做两件事:

  1. 读取字母文件,将每个键、值对(字母、数字值)添加到映射中。
  2. 读取随机单词文件,遍历每个单词中的每个字母,检索它的整数值(从映射中)并将其添加到总和

我提供了一些代码,但您需要自己实现其中的一些代码。

`

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);
}