package com.java_Test;

import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Scanner;
import java.util.Set; public class test {
public static void main(String[] args) throws Exception {
new test().wordCount();
}//统计文本中单词出现的个数
public void wordCount() throws Exception{
File file=new File("text3.txt");
Scanner sc=new Scanner(file);
HashMap<String,Integer> hashMap=new HashMap<>();
System.out.println("文章----------------------");
while(sc.hasNextLine()){
String line=sc.nextLine();
System.out.print(line+" ");
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("单词:%-12s 出现次数:%d\n",word,hashMap.get(word));
}
}
}

java统计文本中单词出现的个数的更多相关文章

  1. shell统计文本中单词的出现次数

    Ubuntu14.04 给定一个文本,统计其中单词出现的次数 方法1 # solution 1 grep与awk配合使用,写成一个sh脚本 fre.sh sh fre.sh wordfretest.t ...

  2. Python 统计文本中单词的个数

    1.读文件,通过正则匹配 def statisticWord(): line_number = 0 words_dict = {} with open (r'D:\test\test.txt',enc ...

  3. Spark——统计文本中单词出现的次数

    示例一:统计所有单词出现的次数 1.在本地创建文件并上传到hdfs中 #vin data.txt //将文件上传到hadoop的根目录下 #hdfs dfs -put data.txt / 2.在sp ...

  4. python统计文本中每个单词出现的次数

    .python统计文本中每个单词出现的次数: #coding=utf-8 __author__ = 'zcg' import collections import os with open('abc. ...

  5. Python的 counter内置函数,统计文本中的单词数量

    counter是 colletions内的一个类 可以理解为一个简单的计数 import collections str1=['a','a','b','d'] m=collections.Counte ...

  6. c程序设计语言_习题1-13_统计输入中单词的长度,并且根据不同长度出现的次数绘制相应的直方图

    Write a program to print a histogram of the lengths of words in its input. It is easy to draw the hi ...

  7. php实现 统计输入中各种字符的个数

    php实现 统计输入中各种字符的个数 一.总结 一句话总结:谋而后动,想清楚,会非常节约编写代码的时间. 1.对结果可能是0的变量,记得初始化? 4 $len=0; 5 $len=strlen($st ...

  8. 设在起始地址为STRING的存储空间存放了一个字符串(该串已存放在内存中,无需输入,且串长不超过99),统计字符串中字符“A”的个数,并将结果显示在屏幕上。

    问题 设在起始地址为STRING的存储空间存放了一个字符串(该串已存放在内存中,无需输入,且串长不超过99),统计字符串中字符"A"的个数,并将结果显示在屏幕上. 代码 data ...

  9. Linux统计文本中某个字符串出现的次数

    常用的有如下两种方式: 1.VIM 用vim打开文件,然后输入: :%s/hello//gn 如下图: 图中的例子就是统计文本中"hello"字符串出现的次数 说明: %s/pat ...

随机推荐

  1. scipy 图像处理(scipy.misc、scipy.ndimage)、matplotlib 图像处理

    from scipy.misc import imread / imsave / imshow imresize / imrotate / imfilter 1. scipy.misc 下的图像处理 ...

  2. IdentityServer4实战 - 谈谈 JWT Token 的安全策略

    原文:IdentityServer4实战 - 谈谈 JWT Token 的安全策略 一.前言 众所周知,IdentityServer4 默认支持两种类型的 Token,一种是 Reference To ...

  3. smarty循环

    1. 功能说明,在页面使用smarty循环100次输出,类似for循环100次{section name=total loop=100}{$smarty.section.total.index+1} ...

  4. this指的是,调用函数的那个对象。

    恩 http://www.ruanyifeng.com/blog/2010/04/using_this_keyword_in_javascript.html

  5. Android设备与外接U盘实现数据读取操作

    现在越来越多手机支持OTG功能,通过OTG可以实现与外接入的U盘等USB设备实现数据传输.关于OTG,可以参考: http://blog.csdn.net/srw11/article/details/ ...

  6. MacOS系统升级后,IDEA的SVN不好用的问题

    最近给MacOS升级到10.13.2(High Sierra). 结果,发现IDEA的SVN不好用了.   提示如下: 查看IDEA的Subversion设置应该是这个样子的:   但是,设置后没有效 ...

  7. MySQL索引 专题

    什么是索引 索引是存储引擎用于快速找到记录的一种数据结构,索引类似一本书的目录,我们可以快速的根据目录查找到我们想要的内容的所在页码,索引的优化应该是对查询性能优化最有效的手段了. 因此,首先你要明白 ...

  8. mvn 命令在command prompt无法识别

    Download maven from this website: https://maven.apache.org/download.cgi 解压binary包后放到一个位置,比如C:\apache ...

  9. mariadb 允许远程访问

    进入MariaDB服务器,将mysql.user的host字段的值改为%就表示在任何客户端机器上能以root用户登录到mysql服务器,建议在开发时设为%. 1 MariaDB [(none)]> ...

  10. Win8Metro(C#)数字图像处理--2.33图像非线性变换

    原文:Win8Metro(C#)数字图像处理--2.33图像非线性变换  [函数名称] 图像非线性变换函数NonlinearTransformProcess(WriteableBitmap src ...