java中,给定一个文本,统计其中的单词个数,要求以单词在文本中出现的先后顺序输出
2个回答
展开全部
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.PrintWriter;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class WordCount {
public static void main(String[] args) throws Exception {
File file = new File("1.txt");
PrintWriter pw = new PrintWriter(file);
pw.println("hello world, hello java.");
pw.println("hello program, hello java.");
pw.close();
printWords(file);
}
public static void printWords(File file) throws Exception {
BufferedReader br = new BufferedReader(new FileReader(file));
Pattern pattern = Pattern.compile("[A-Za-z]+");
Map<String, Integer> map = new LinkedHashMap<>();
while (br.ready()) {
Matcher matcher = pattern.matcher(br.readLine());
while (matcher.find()) {
String word = matcher.group();
if (!map.containsKey(word)) {
map.put(word, 0);
}
map.put(word, map.get(word) + 1);
}
}
br.close();
for (String word : map.keySet()) {
System.out.println(word + " : " + map.get(word));
}
}
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询