zoukankan      html  css  js  c++  java
  • 英语单词统计

    能够实现单词个数统计和字母个数统计 并且将这些导入文本中

    public static void main(String[] args)throws IOException//扔掉很重要
    {
          // 新文件
            File newFile = new File("E:\\eclipse-workspace\\2.0\\src\\英语单词统计\\计算结果.txt");
            int count_xiao = 0,count_da=0;
            // 读取文件内容
            File file = new File("E:\\eclipse-workspace\\2.0\\src\\英语单词统计\\Harry Potter.txt");
            BufferedReader reader = new BufferedReader(new FileReader(file));//读取数据
            StringBuffer sb = new StringBuffer();//对字符串进行修改 并且可以进行多次修改 并且不产生新的未使用对象
            String line = null;//行数
            while ((line = reader.readLine()) != null) {
                sb.append(line);//之间的简单相加
            }
            reader.close();
            // 统计大小写字母个数
           
            for (int i = 0; i < sb.length(); i++) {
                char ch = sb.charAt(i);//读取第i个字母
                if ((ch >= 'a' && ch <= 'z') ) {
                    count_xiao ++;
                }
                else if ( (ch >= 'A' && ch <= 'Z')) {
                    count_da ++;
                }
            }
            System.out.println("小写个数:"+count_xiao);
            System.out.println("大写个数:"+count_da);
            //计算单词个数
            int sum_dan=1;
            for (int i = 0; i < sb.length(); i++) {
                char ch = sb.charAt(i);//读取第i个字母
                //如果有空格逗号句号空格换行冒号就是一个新的单词
                if (ch<'A'||(ch>'Z'&&ch<'a')||ch>'z'){
                    sum_dan ++;
                    char c=sb.charAt(i+1);
                    if (c<'A'||(c>'Z'&&c<'a')||c>'z')
                    {
                     sum_dan--;
                    }
                }
               
            }
            System.out.println("单词个数:"+sum_dan);
            String s;
            String[] buffer=null ;
            int[] sum_geshu;
            sum_geshu=new int[(int) sum_dan];
            for (int i = 0; i < sb.length(); i++)
            {   s=reader.readLine();
          while(s!=null)
          {buffer=s.split("\\s+");
          }
            }
            reader.close();
            for(int i=0;i<sum_dan;i++)
            {
             for(int j=i+1;j<sum_dan;j++)
             {
              if(buffer[i]==buffer[j])
              {
               sum_geshu[i]=0;
               sum_geshu[i]=sum_geshu[i]+1;
               buffer[j]=null;
               sum_geshu[j]=0;
              }
             }
            }
            for(int i=0;i<sum_dan;i++)
            {
             System.out.print(buffer[i]+":"+sum_geshu[i]);
            }
            FileOutputStream out = new FileOutputStream(newFile);
            out.write(String.valueOf(count_xiao).getBytes());
            out.write(String.valueOf(count_da).getBytes());
            out.write(String.valueOf(sum_dan).getBytes());
            out.flush();
            out.close();
    }
  • 相关阅读:
    现代软件工程 第一章 概论 第3题——韩婧
    现代软件工程 第一章 概论 第2题——韩婧
    小组成员邓琨、白文俊、张星星、韩婧
    UVa 10892 LCM的个数 (GCD和LCM 质因数分解)
    UVa 10780 幂和阶乘 求n!中某个因子的个数
    UVa 11859 除法游戏(Nim游戏,质因子)
    Codeforces 703C Chris and Road 二分、思考
    Codeforces 703D Mishka and Interesting sum 树状数组
    hdu 5795 A Simple Nim SG函数(多校)
    hdu 5793 A Boring Question 推公式(多校)
  • 原文地址:https://www.cnblogs.com/smartisn/p/9775548.html
Copyright © 2011-2022 走看看