zoukankan      html  css  js  c++  java
  • 【Java例题】7.6文件题3-文本文件统计

    6.文本文件统计。
    已有一个文本文件文件,请统计数字、大写字母、小写字母、汉字及其它字符出现的次数;
    然后将这些次数由大到小写到另一个文件之中。
    说明:将次数为零的过滤掉排序

    package chapter7;
    
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.PrintStream;
    import java.util.Scanner;
    
    public class demo6 {
        public static void main(String[] args) {
            int da=0;
            int xiao=0;
            int shu=0;
            int han=0;
            int els=0;
            try {
                Scanner sc = new Scanner(new File("strings.txt"));
                PrintStream out=new PrintStream(new File("cord.txt"));
                String str=sc.next();
                for(int i=0;i<str.length();i++) {
                    char c=str.charAt(i);
                    if(c>='A'&&c<='Z') {
                        da=da+1;
                    }else if(c>='a'&&c<='z'){
                        xiao=xiao+1;
                    }else if(c>='0'&&c<='9') {
                        shu=shu+1;
                    }else if(c>=0x4E00&&c<=0x9FA5) {
                        han=han+1;
                    }else {
                        els=els+1;
                    }
                }
                int a[]= {da,xiao,shu,han,els};
                num b[]=new num[5];
                for(int i=0;i<5;i++) {
                    b[i]=new num(i);
                    b[i].value=a[i];
                }
                for(int i=0;i<5;i++) {
                    for(int j=i+1;j<5;j++) {
                        if(b[i].value>b[j].value) {
                            num temp=b[j];
                            b[j]=b[i];
                            b[i]=temp;
                        }
                    }
                }
                for(int i=0;i<5;i++) {
                    out.print(b[i].shownum()+" ");
                }
                sc.close();
            }catch (FileNotFoundException e) {
                // TODO 自动生成的 catch 块
                e.printStackTrace();
            }
        }
        
        static class num{
            int type;
            int value;
            public num(int i) {
                this.type=i;
            }
            String shownum() {
                if(value==0) {
                    return "";
                }
                switch(type) {
                    case 0:return("大写数量:"+value);
                    case 1:return("小写数量:"+value);
                    case 2:return("数字数量:"+value);
                    case 3:return("汉字数量:"+value);
                    case 4:return("其他数量:"+value);
                    default:return "";
                }
            }
        }
    }
  • 相关阅读:
    开挂的列表与矜持的元组
    烦人的字符串
    好用的for循环与range
    浅谈编码
    流程控制与循环
    基础运算符
    python初识
    python的小介绍
    来自极客标签10款最新设计素材-系列九
    chmod----改变一个或多个文件的存取模式(mode)
  • 原文地址:https://www.cnblogs.com/LPworld/p/10724116.html
Copyright © 2011-2022 走看看