zoukankan      html  css  js  c++  java
  • 统计英文字母出现次数

    源码:

    package yingwen;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.InputStreamReader;
    public class yingwen {
     private int num[]=new int[52];
     public void readTxt(String filePath) {
      try {
       File file = new File(filePath);
       if(file.isFile() && file.exists()) {
        InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "utf-8");
        while(isr.ready())
        {
         char c=(char)isr.read();
         if(c>='a'&&c<='z')
         {
          ++num[(int)(c-'a')];
         }
         else if(c>='A'&&c<='Z')
         {
          ++num[(int)(c-'A')];
         }
        }
        //         BufferedReader br = new BufferedReader(isr);
        //         String lineTxt = null;
        //         while ((lineTxt = br.readLine()) != null) {
        //           System.out.println(lineTxt);
        //         }
        //         br.close();
        isr.close();
       } else {
        System.out.println("文件不存在!");
       }
      } catch (Exception e) {
       System.out.println("文件读取错误!");
      }
     }
     public int[] num1()
     {
      return num;
     }
     public static void main(String[] args) {
      String filePath = "E:\Yingwen\English.txt";
      yingwen a=new yingwen();
      a.readTxt(filePath);
      int num[]=a.num1();
      for(int i=0;i<52;i++)
      {
       if(i<=25)
        System.out.println((char)(i+'a')+" "+num[i]);
       else System.out.println((char)(i+'A'-26)+" "+num[i]);
      }
     }
    }

     
  • 相关阅读:
    Spring不再推荐使用字段注入
    谈谈Web安全色(web safe color)
    mybatis写mapper文件注意事项
    GitHub下载加速
    ssm项目 ajax发起post请求,控制台一直404
    第一篇随笔
    C语言编程练习64:还是畅通工程
    C语言编程练习63:完数
    C语言编程练习62:汉诺塔3
    C语言编程练习61:Tempter of the bone
  • 原文地址:https://www.cnblogs.com/990906lhc/p/9787561.html
Copyright © 2011-2022 走看看