分析:

1)要统计单词的个数,就自己的对文章中单词出现的判断的理解来说是:当出现一个非字母的字符的时候,对前面的一部分字符串归结为单词

2)对于最后要判断字母出现的个数这个问题,我认为应该是要用到map比较合适吧,因为map中有 键-值 的关系,可以把字符串设置为键,把出现的个数设置为整型,这样就能够建立起一一对应的关系,不用再判断所在的位置

根据上面自己的理解,今天我写了以下的一部分代码,对哈利波特第一集的这部分文章进行了单词的统计的测试,测试的结果相对良好,没有问题。

package pipei;
//洪鼎淇 20173627 信1705-3
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.HashMap;
import java.util.Map;
//哈利波特单词统计 public class Pipei {
public Map<String,Integer> map1=new HashMap<String,Integer>();
public static void main(String arg[]) {
String sz[];
Integer num[];
final int MAXNUM=10; //统计的单词出现最多的前n个的个数 sz=new String[MAXNUM+1];
num=new Integer[MAXNUM+1];
Pipei pipei=new Pipei();
int account =1;
//Vector<String> ve1=new Vector<String>();
try {
pipei.daoru();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
System.out.println("英文单词的出现情况如下:");
int g_run=0; for(g_run=0;g_run<MAXNUM+1;g_run++)
{
account=1;
for(Map.Entry<String,Integer> it : pipei.map1.entrySet())
{
if(account==1)
{
sz[g_run]=it.getKey();
num[g_run]=it.getValue();
account=2;
}
if(account==0)
{
account=1;
continue;
}
if(num[g_run]<it.getValue())
{
sz[g_run]=it.getKey();
num[g_run]=it.getValue();
}
//System.out.println("英文单词: "+it.getKey()+" 该英文单词出现次数: "+it.getValue());
}
pipei.map1.remove(sz[g_run]);
}
int g_count=1;
String tx1=new String();
for(int i=0;i<g_run;i++)
{
if(sz[i]==null)
continue;
if(sz[i].equals(""))
continue;
tx1+="出现次数第"+(g_count)+"多的单词为:"+sz[i]+"\t\t\t出现次数: "+num[i]+"\r\n";
System.out.println("出现次数第"+(g_count)+"多的单词为:"+sz[i]+"\t\t\t出现次数: "+num[i]);
g_count++;
}
try {
pipei.daochu(tx1);
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} }
public void daoru() throws IOException
{ File a=new File("1.Harry Potter and the Sorcerer's Stone.txt");
FileInputStream b = new FileInputStream(a);
InputStreamReader c=new InputStreamReader(b,"UTF-8");
String string2=new String();
while(c.ready())
{
char string1=(char) c.read();
if(!isWord(string1))
{
if(map1.containsKey(string2))
{
Integer num1=map1.get(string2)+1;
map1.put(string2,num1);
}
else
{
Integer num1=1;
map1.put(string2,num1);
}
string2="";
}
else
{
string2+=string1;
}
}
if(!string2.isEmpty())
{
if(map1.containsKey(string2))
{
Integer num1=map1.get(string2)+1;
map1.put(string2,num1);
}
else
{
Integer num1=1;
map1.put(string2,num1);
}
string2="";
}
c.close();
b.close();
}
public void daochu(String txt) throws IOException
{
File fi=new File("tongji.txt");
FileOutputStream fop=new FileOutputStream(fi);
OutputStreamWriter ops=new OutputStreamWriter(fop,"UTF-8");
ops.append(txt);
ops.close();
fop.close();
}
public boolean isWord(char a)
{
if(a<='z'&&a>='a'||a<='Z'&&a>='A')
return true;
return false;
} }

测试截图:

这是出现的单词的截图情况,对于其中出现s的情况的分析,s其实是单词里面的缩写,一般往往有It's 这种情况的出现,为了避免这种情况,我对文中的某一部分进行修改(修改部分如下图),将It's看成一部分

isWord函数判断是否为字母的这一部分将单引号也作为一个符号放进去

修改完之后的测试如下:

搞定!!

JAVA实验--统计文章中单词的个数并排序的更多相关文章

  1. 统计文件中单词的个数---Shell及python版

    最近在看shell中有个题目为统计单词的个数,使用了awk功能,代码如下 #!/bin/bash ];then echo "Usage:basename $0 filename" ...

  2. C语言算法--统计字符串中单词的个数

    #include <stdio.h> #include <string.h> #include <stdlib.h> int main(void) { int le ...

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

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

  4. 使用tuple统计文件中单词的个数

    name = input("Enter file:") if len(name) < 1 : name = "input.txt" fhand = ope ...

  5. java统计文本中单词出现的个数

    package com.java_Test; import java.io.File; import java.util.HashMap; import java.util.Iterator; imp ...

  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. 学c语言做练习之​统计文件中字符的个数

    统计文件中字符的个数(采用命令行参数) #include<stdio.h> #include<stdlib.h> int main(int argc, char *argv[] ...

  8. 统计无向图中三角形的个数,复杂度m*sqrt(m).

    统计无向图中三角形的个数,复杂度m*sqrt(m). #include<stdio.h> #include<vector> #include<set> #inclu ...

  9. C语言 统计一篇英文短文中单词的个数

    //凯鲁嘎吉 - 博客园 http://www.cnblogs.com/kailugaji/ #include<stdio.h> #define N 1000 void main(){ ] ...

随机推荐

  1. Oracle的Central Inventory和Local inventory详解

    很多朋友对Oracle的inventory信息不太了解以至遇到相关的问题不知道如何处理,这篇文章我们将详细讲解Oracle的Central Inventory (oraInventory)和Local ...

  2. System类与两种输入流

    1.System类对I/O的支持系统输出System.out.println 是利用了I/O流的模式完成的.实际是打印流PrintStream对象 System类中定义了三个操作的常量 1.标准/系统 ...

  3. C-基础:冒泡排序

    #include <fstream> #include <iostream.h> //--------------------------------------------- ...

  4. 通俗理解 模糊自适应PID

    模糊自适应PID算法就是在经典的PID的基础上添加模糊控制规则库,建立这个库的目的就是算法能够自己来进行改变P.I.D的值. 就拿温度的上升过程控制来说,刚开始的时候,希望温度能够快速的升到终点温度, ...

  5. Perl语言入门: 斜线不是元字符,所以在不作为分隔符时不需要加上反斜线。

    Perl语言入门: 斜线不是元字符,所以在不作为分隔符时不需要加上反斜线.

  6. 《3+1团队》【Alpha】Scrum meeting 1

    项目 内容 这个作业属于哪个课程 任课教师博客主页链接 这个作业的要求在哪里 作业链接地址 团队名称 3+1团队 团队博客地址 https://home.cnblogs.com/u/3-1group ...

  7. Mac下Apache服务器的初步搭建

    回送地址  127.0.0.1(localhost)  ping 这个地址可以检测网卡是否正常 ping 本地地址如果不正常说明网线挂了   // 启动 sudo apachectl -k start ...

  8. C字符串指针遇到的问题

    看下面的示例代码: int main() { char *ptr = "GeeksQuiz"; printf("%c\n", *&*&*ptr) ...

  9. css 浮动规则

    1. 如果浮动元素的高度大于父级元素,那么浮动元素块会超过父级元素的底部.解决办法:将父级元素也设置为浮动定位. 2. 不改变box尺寸的情况下增加box内部的内边距:box-sizing: bord ...

  10. 含有通配符*的字符匹配(C语言)

    含有通配符的字符匹配,采用贪心算法 //1 -> true //0 -> false int IsMatch(const char* reg, const char *str) { int ...