gpt4 book ai didi

java - 计算文件中与 String [ ] 中的单词匹配的单词数

转载 作者:行者123 更新时间:2023-11-30 02:36:13 25 4
gpt4 key购买 nike

我正在编写一个程序来读取文件并计算该文件中特定单词的出现次数。

我已经让代码工作到一定程度了。我把我想要统计的单词放在一个String[]中。问题是该程序要么计算文件中所有单词的出现次数(包括我不想计算的单词),要么计算 String [] 中的单词。

如何让程序计算文件中与数组中的单词匹配的单词数?我浏览了许多类似的问题,并尝试使用 StringTokenizer 和列表,但也无法让它们完全工作。

我的目标是,如果我的文件包含文本“黄红蓝白黑紫蓝”,我希望我的输出为“红:1,蓝:2,黄:1”

我只是想朝着正确的方向插入,我知道我坚持下去是愚蠢的,并且一如既往,任何建设性的反馈都会受到赞赏。

这是迄今为止我的代码:

static String[] words = { "red", "blue", "yellow", "green" };

public static void main(String[] args) throws FileNotFoundException, IOException {

System.out.println("This program will count the occurences of the specific words from a text file.");

System.out.println("\nThe words to be counted are; red, blue, yellow, and green.\n");

Map map = new HashMap();

try (BufferedReader br = new BufferedReader(new FileReader("colours.txt"))) {

StringBuilder sb = new StringBuilder();

String line = br.readLine();

while (line != null) {

words = line.split(" "); // keeping this counts all words separated by whitespace, removing it counts words in my array instead of the file, so I'll get red: 1, blue: 1, yellow: 1 etc.,

for (int i = 0; i < words.length; i++) {

if (map.get(words[i]) == null) {

map.put(words[i], 1);
}

else {

int newValue = Integer.valueOf(String.valueOf(map.get(words[i])));

newValue++;

map.put(words[i], newValue);
}

}

sb.append(System.lineSeparator());

line = br.readLine();
}
}

Map<String, String> sorted = new TreeMap<String, String>(map);

for (Object key : sorted.keySet()) {

System.out.println(key + ": " + map.get(key));
}
}

最佳答案

上面的主要问题是您正在覆盖初始数组或 words当您拆分刚刚阅读的行时。

我写了这个(为了我自己的理解稍微修改了变量名称)

(根据评论更新,感谢@shmosel)

public static void main(String[] args) throws FileNotFoundException, IOException {

String[] keywords = {"red", "blue", "yellow", "green"};
// for easier querying contents of array
List keywordList = Arrays.asList(keywords);

System.out.println("This program will count the occurrences of the specific words from a text file.");
System.out.println("\nThe words to be counted are: " + keywordList + ".\n");

Map<String, Integer> wordMap = new HashMap<>();

try (BufferedReader br = new BufferedReader(new FileReader("/path/to/file/colours.txt"))) {
// read a line
String line = br.readLine();

while (line != null) {
// keeping this counts all words separated by whitespace, removing it counts words in my array instead
// of the file, so I'll get red: 1, blue: 1, yellow: 1 etc.,
String[] words = line.split(" ");

for(String oneWord : words ){
if( keywordList.contains(oneWord)){
// thanks @ shmosel for the improvement suggested in comments
wordMap.merge(oneWord, 1, Integer::sum);
}
}

line = br.readLine();
}
}

Map<String, Integer> sorted = new TreeMap<>(wordMap);

for (Object key : sorted.keySet()) {
System.out.println(key + ": " + wordMap.get(key));
}
}

关于java - 计算文件中与 String [ ] 中的单词匹配的单词数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43010925/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com