使用哈希映射的字数计算程序 import java.io.*; 导入java.util.*; 公共类ListSetMap2 { 公共静态void main(字符串[]args) { Map my_collection=new HashMap(); 扫描仪键盘=新扫描仪(System.in); System.out.println(“输入文件名”); 字符串filenameString=keyboard.nextLine(); 文件名=新文件(filenameString); int word_位置=1; int word_num=1; 尝试 { 扫描仪数据存储=新扫描仪(文件名); System.out.println(“打开”+文件名字符串); while(data_store.hasNext()) { String word=data_store.next(); if(word.length()>5) { if(我的收藏.containsKey(word)) { 我的收藏.get(我的收藏.containsKey(word)); 整数p=(整数)my_collection.get(word_num++); 我的收藏。放(字,p); } 其他的 { 整数i=(整数)我的集合.get(单词数); 我的收藏。放(字,我); } } } } catch(filenotfounde异常) { System.out.println(“不!”); } } }
我正在尝试编写一个程序,它输入/扫描一个文件,将单词记录在HashMap集合中,然后计算单词在文档中出现的次数,只计算超过5个字符的单词 这中间有点乱,但我会讨论如何计算单词出现的次数,并为每个单词保持一个独立的计数。我相信这里有一个简单的解决方案,我只是错过了。请帮忙 (只给出提示,因为这似乎是家庭作业。)使用哈希映射的字数计算程序 import java.io.*; 导入java.util.*; 公共类ListSetMap2 { 公共静态void main(字符串[]args) { Map my_collection=new HashMap(); 扫描仪键盘=新扫描仪(System.in); System.out.println(“输入文件名”); 字符串filenameString=keyboard.nextLine(); 文件名=新文件(filenameString); int word_位置=1; int word_num=1; 尝试 { 扫描仪数据存储=新扫描仪(文件名); System.out.println(“打开”+文件名字符串); while(data_store.hasNext()) { String word=data_store.next(); if(word.length()>5) { if(我的收藏.containsKey(word)) { 我的收藏.get(我的收藏.containsKey(word)); 整数p=(整数)my_collection.get(word_num++); 我的收藏。放(字,p); } 其他的 { 整数i=(整数)我的集合.get(单词数); 我的收藏。放(字,我); } } } } catch(filenotfounde异常) { System.out.println(“不!”); } } },java,algorithm,hashmap,counting,Java,Algorithm,Hashmap,Counting,我正在尝试编写一个程序,它输入/扫描一个文件,将单词记录在HashMap集合中,然后计算单词在文档中出现的次数,只计算超过5个字符的单词 这中间有点乱,但我会讨论如何计算单词出现的次数,并为每个单词保持一个独立的计数。我相信这里有一个简单的解决方案,我只是错过了。请帮忙 (只给出提示,因为这似乎是家庭作业。)my_collection是(正确的)一个HashMap,它将字符串键映射到整数值;在您的情况下,一个键应该是一个单词,相应的值应该是您看到该单词的次数(频率)。每次调用my_collect
my_collection
是(正确的)一个HashMap
,它将字符串
键映射到整数
值;在您的情况下,一个键应该是一个单词,相应的值应该是您看到该单词的次数(频率)。每次调用my_collection.get(x)
,参数x
都需要是一个字符串,即您想要知道其频率的单词(不幸的是,HashMap
没有强制执行此操作)。每次调用my_collection.put(x,y)
,x
需要是字符串
,y
需要是整数
或int
,即该单词的频率
考虑到这一点,请仔细考虑使用什么作为参数,调用的顺序以及如何操作值。例如,如果您已经确定my_collection
不包含该单词,那么询问my_collection
该单词的频率有意义吗?如果它确实包含单词,在将新值放入“我的收藏”之前,您需要如何更改频率
(另外,请为my_collection
选择一个更具描述性的名称,例如frequencies
)(仅给出提示,因为这似乎是家庭作业)。my_collection
是(正确地)将字符串
键映射到整数
值的哈希映射图
;在您的情况下,一个键应该是一个单词,相应的值应该是您看到该单词的次数(频率)。每次调用my_collection.get(x)
,参数x
都需要是一个字符串,即您想要知道其频率的单词(不幸的是,HashMap
没有强制执行此操作)。每次调用my_collection.put(x,y)
,x
需要是字符串
,y
需要是整数
或int
,即该单词的频率
考虑到这一点,请仔细考虑使用什么作为参数,调用的顺序以及如何操作值。例如,如果您已经确定my_collection
不包含该单词,那么询问my_collection
该单词的频率有意义吗?如果它确实包含单词,在将新值放入“我的收藏”之前,您需要如何更改频率
(另外,请为my_collection
选择一个更具描述性的名称,例如frequencies
)(仅给出提示,因为这似乎是家庭作业)。my_collection
是(正确地)将字符串
键映射到整数
值的哈希映射图
;在您的情况下,一个键应该是一个单词,相应的值应该是您看到该单词的次数(频率)。每次调用my_collection.get(x)
,参数x
都需要是一个字符串,即您想要知道其频率的单词(不幸的是,HashMap
没有强制执行此操作)。每次调用my_collection.put(x,y)
,x
需要是字符串
,y
需要是整数
或int
,即该单词的频率
考虑到这一点,请仔细考虑使用什么作为参数,调用的顺序以及如何操作值。例如,如果您已经确定my_collection
不包含该单词,那么询问my_collection
该单词的频率有意义吗?如果它确实包含单词,在将新值放入“我的收藏”之前,您需要如何更改频率
(另外,请为my选择更具描述性的名称。)_
import java.io.*;
import java.util.*;
public class ListSetMap2
{
public static void main(String[] args)
{
Map<String, Integer> my_collection = new HashMap<String, Integer>();
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter a file name");
String filenameString = keyboard.nextLine();
File filename = new File(filenameString);
int word_position = 1;
int word_num = 1;
try
{
Scanner data_store = new Scanner(filename);
System.out.println("Opening " + filenameString);
while(data_store.hasNext())
{
String word = data_store.next();
if(word.length() > 5)
{
if(my_collection.containsKey(word))
{
my_collection.get(my_collection.containsKey(word));
Integer p = (Integer) my_collection.get(word_num++);
my_collection.put(word, p);
}
else
{
Integer i = (Integer) my_collection.get(word_num);
my_collection.put(word, i);
}
}
}
}
catch (FileNotFoundException e)
{
System.out.println("Nope!");
}
}
}
// if the word is already present in the hashmap
if (my_collection.containsKey(word)) {
// just increment the current frequency of the word
// this overrides the existing frequency
my_collection.put(word, my_collection.get(word) + 1);
} else {
// since the word is not there just put it with a frequency 1
my_collection.put(word, 1);
}
while(data_store.hasNext()) {
String word = data_store.next();
if(word.length() > 5){
if(my_collection.get(word)==null) my_collection.put(1);
else{
my_collection.put(my_collection.get(word)+1);
}
}
}