在Java中使用set和map从文件中读取特定关键字
我创建了一个包含文本的文件。我想用在Java中使用set和map从文件中读取特定关键字,java,file,map,set,Java,File,Map,Set,我创建了一个包含文本的文件。我想用set来存储这些关键字,并用map来显示所有关键字和重复的次数,来阅读诸如“end”、“so”和“because”之类的特定单词。你能告诉我该怎么做吗 编辑 这是我的草稿: openButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JFileChooser fileChooser = new JFil
set
来存储这些关键字,并用map
来显示所有关键字和重复的次数,来阅读诸如“end”、“so”和“because”之类的特定单词。你能告诉我该怎么做吗
编辑
这是我的草稿:
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”);
openButton.addActionListener(新ActionListener(){
已执行的公共无效操作(操作事件e){
JFileChooser fileChooser=新的JFileChooser();
int chosenFile=fileChooser.showOpenDialog(null);
如果(chosenFile==JFileChooser.APPROVE_选项){
File selectedFile=fileChooser.getSelectedFile();
字符串扩展名=getExtension(selectedFile.getName());
如果(selectedFile.canRead())
Set关键字列表=新HashSet();k
关键词列表。添加(“和”);
关键词列表。添加(“so”);
关键词列表。添加(“因为”);
我不知道如何从这里开始使用map来定义关键字I aggree to 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
}
}
}
}
循环浏览文件的每一行(使用扫描仪)
{
使用空格作为分线器拆分当前线路并生成数组(使用String.Split函数)
循环遍历数组中的每个字
{
查看单词是否在集合中(使用集合的contains方法)
如果在集合中
{
查看单词是否在map中(使用map.get方法)
如有
{
放入地图
}
其他的
{
使用get方法从地图中获取单词的当前名称。
增加一个。
放入地图
}
}
}
}
现在还没有开始点吗?你能至少提供一个你计划使用的代码吗?我创建了一个用户界面,其中有一个按钮“查找文件并选择它”和一个显示结果的文本区域。我创建了一个GUI,其中一个按钮用于选择文件,一个文本区域用于显示结果。openButton.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){JFileChooser fileChooser=new JFileChooser();感谢您添加Emecas.openButton.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){JFileChooser fileChooser=new JFileChooser();int chosenFile=fileChooser.showOpenDialog(null);if(chosenFile==JFileChooser.APPROVE_选项){File selectedFile=fileChooser.getSelectedFile();String extension=getExtension(selectedFile.getName());if(selectedFile.canRead())Set keywordList=new HashSet();keywordList.add(“and”);keywordList.add(“so”);keywordList.add(“因为”);我不知道如何从这里开始使用map来优化关键字我不能以一种好的方式发布代码,因为网站不会让me@Marocain是的,你可以。你只是不能在评论中这样做。相反,你应该编辑你的问题。有人已经为你这样做了。对不起,我是这个网站的新手。谢谢你的提示。