我创建了一个包含文本的文件。我想阅读特定的字词,例如“结束”,“如此”和“因为”使用set
来存储这些关键字,并使用map
来显示所有关键字和重复的次数。你能告诉我应该怎么做吗?
EDITED
这是我的草稿:
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来细化关键字
答案 0 :(得分: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
}
}
}
}