WordConuts
import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Scanner;
import java.util.Set; public class 统计单词
{
public static void main(String[] args) throws FileNotFoundException
{
File file=new File("C:\\Users\\hp\\eclipse-workspace\\English.txt");
if(!file.exists())
{
System.out.println("文件不存在");
return;
}
Scanner scanner=new Scanner(file);
//单词和数量映射表
HashMap<String, Integer > hashMap=new HashMap<String,Integer>();
System.out.println("文章-----------------------------------");
while(scanner.hasNextLine())
{
String line=scanner.nextLine();
System.out.println(line);
//\w+ : 匹配所有的单词
//\W+ : 匹配所有非单词
String[] lineWords=line.split("\\W+");//用非单词符来做分割,分割出来的就是一个个单词 Set<String> wordSet=hashMap.keySet();
for(int i=0;i<lineWords.length;i++)
{
//如果已经有这个单词了,
if(wordSet.contains(lineWords[i]))
{
Integer number=hashMap.get(lineWords[i]);
number++;
hashMap.put(lineWords[i], number);
}
else
{
hashMap.put(lineWords[i], 1);
}
} }
System.out.println("统计单词:------------------------------");
Iterator<String> iterator=hashMap.keySet().iterator();
while(iterator.hasNext())
{
String word=iterator.next(); // System.out.printf("单词: "+word+"出现次数:"+hashMap.get(word));
System.out.printf("单词:%-12s 出现次数:%d\n",word,hashMap.get(word));
} System.out.println("程序结束--------------------------------");
}
}
运行结果:
文章-----------------------------------
a a a a aa aa aaa aaa aaaa aaaa
统计单词:------------------------------
单词: 出现次数:1
单词:aa 出现次数:2
单词:aaa 出现次数:2
单词:a 出现次数:4
单词:aaaa 出现次数:2
程序结束--------------------------------
WordConuts的更多相关文章
随机推荐
- MySQL5.7 多实例
1,先按MySQL5.7安装好后 mkdir dbdata_3307 chown mysql:mysql -R dbdata_3307 2,./bin/mysqld --initialize --u ...
- 使用SBT构建Scala应用【转载】
使用SBT构建Scala应用 SBT简介 SBT是Simple Build Tool的简称,如果读者使用过Maven,那么可以简单将SBT看做是Scala世界的Maven,虽然二者各有优劣,但完成的工 ...
- PHP的类,abstract类,interface及关键字extends和implements
原文:https://blog.csdn.net/qq_19557947/article/details/77880757?locationNum=4&fps=1 PHP类 PHP类是单继承, ...
- CSS3-Hover 效果 展示
首先说说兼容性的问题吧,主要说说IE浏览器的兼容性.IE10+. 效果展示的css代码: .button { margin: .4em; padding: 1em; cursor: pointer; ...
- WCG distribution of byteball
https://steemit.com/byteball/@punqtured/processing-for-good-get-rewarded-for-crunching-numbers-to-cu ...
- 如何创建线程第一种继承Thread类
步骤 1:定义一个类 继承Thread类.2:重写Thread类的run方法.3:直接创建Thread的子类对象创建线程.4:调用start方法开启线程并调用线程的任务run方法执行.-------- ...
- Java 基础 常用API (System类,Math类,Arrays, BigInteger,)
基本类型包装类 基本类型包装类概述 在实际程序使用中,程序界面上用户输入的数据都是以字符串类型进行存储的.而程序开发中,我们需要把字符串数据,根据需求转换成指定的基本数据类型,如年龄需要转换成int类 ...
- [LeetCode] 35. Search Insert Position_Easy tag: Binary Search
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- AFNetworking 源码解析
3.0 之后,就取消了NSOperation的控制. 因为根据Apple Developer Document的文档 https://developer.apple.com/documentation ...
- 延期版本webstorm(解决许可证过期,注册,激活,破解,码,支持正版,最新可用)
很多人都发现 http://idea.lanyus.com/ 不能激活了 很多帖子说的 http://15.idea.lanyus.com/ 之类都用不了了 选择 License server (20 ...