-3

我创建了一个包含文本的文件。我想读取特定的单词,如“end”、“so”和“because”,用set​​于存储这些关键字,并map用于显示所有关键字和重复次数。你能告诉我我应该怎么做吗?

已编辑

这是我的草稿:

openButton.addActionListener(new ActionListener() { 
    public void actionPerformed(ActionEvent e) { 
        JFileChooser fileChooser = new JFileChooser(); 
        int chosenFile = fileChooser.showOpenDialog(null);
        if(chosenFile == JFileChooser.APPROVE_OPTION){ 
            File selectedFile = fileChooser.getSelectedFile(); 
            String extension = getExtension(selectedFile.getName()); 
            if ( selectedFile.canRead()) 
                Set<String> keywordList = new HashSet<String>();k
                keywordList.add("and"); 
                keywordList.add("so"); 
                keywordList.add("because”);

我不知道如何从这里使用 map 来细化关键字

4

1 回答 1

2

我同意 emecas....请提供一些代码...这不是您可以使用现成代码的地方...

这是供您使用的伪代码..

Loop through each line of file (use Scanner)
{
    Split current line using space as delimeter and make array ( Use String.split function)
    Loop through each word of array  
    {
        See if word is in Set( using contains method of set)
        If in set
        {
            see if word is in map (use map.get method)
            if present
            {
                put <word,1> in map
            }
            else
            {
                use get method to get current cound of word from map.
                Increment by one.
                put <word,incremented count> in map
            }
        }

    }
}
于 2013-03-08T00:44:56.250 回答