文章单词统计--测试报告

1.文件打开,文档的录入,没有问题。

2.文件的读取,存储到数组结构体中没有问题。

3.数组中单词个数的统计,存在bug暂时还没解决,预期的是,将空格作为分隔符,一个一个读取单词,依次查重,重复的,单词个数加1,不重复的,新建一个结构体,存入。但运行结果出现误差,统计中断,在调试中。

创建文件:

 {
            
            String path= "c:\文章\统计";//所创建文件的路径
            
            File f = new File(path);
            
           if(!f.exists()){
                
                f.mkdirs();//创建目录
            }
            
            String fileName = "abc.txt";//文件名及类型
            
            File file = new File(path, fileName);
            
            if(!file.exists()){
                
                try {
                    file.createNewFile();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                
            }
           
        }            

输入文件:

public void shuruFile()//输入文本
       {
         Scanner in=new Scanner(System.in); 
         try {
            FileWriter fw = new FileWriter("c:\文章\统计\abc.txt");
            String world;
            
            world=in.nextLine();
            fw.write(world);
            
               fw.close();
               } catch (IOException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
             }
    
  
       }

读取文本

public String duqufile()//读取文本
    {String s = null;
        File f= new File("c:\文章\统计" + File.separator + "abc.txt") ;    // 声明File对象
                 // 第2步、通过子类实例化父类对象
                 Reader input = null ;    // 准备好一个输入的对象
                 try {
                    input = new FileReader(f)  ;
                } catch (FileNotFoundException e) {
                    // TODO 自动生成的 catch 块
                    e.printStackTrace();
                }    // 通过对象多态性,进行实例化
                 // 第3步、进行读操作
                 char c[] = new char[1024] ;        // 所有的内容都读到此数组之中
                 try {
                    int len = input.read(c) ;
                    s=String.valueOf(c);
                       } catch (IOException e) {
                    // TODO 自动生成的 catch 块
                    e.printStackTrace();
                }        // 读取内容
                // 第4步、关闭输出流
                 try {
                    input.close() ;
                } catch (IOException e) {
                    // TODO 自动生成的 catch 块
                    e.printStackTrace();

                    }    
                 
              
    return s;}

统计文本

 public void tongjufile(String s)//统计文本
     {
        String[] a=new String [10000];
        int[] b=new int [1000];
        int n=0;//一共的字母
         int k=0;//单词
    char c[] = s.toCharArray();
         for(;c[n]!=''; n++)//将文本中单词存入a[]中
         {
             int j=0;
             for(j=n;c[j]!=' ';j++)
             {
                 
             }
            a[k]=s.substring(n,j);b[k]=1;n=j;
             for(int i=0;i<k;i++)
                 if(a[i].equals(a[k])) 
                     {b[i]++;k--;break;}
                k++; 
             
            }
         k--;
         danci[] z=new danci[k];//创建类将单词和个数联系起来
         
            for(int i=0;i<=k;i++)
            {
                z[i].num=b[i];
                z[i].world=a[i];
            }
            danci t = null,m = null;
        for(int i=0;i<k;i++)    
        {
            for(int j=i;j<=k;j++)
            {
                if(z[j].num<z[i].num)
                {
                    m.deng(t,z[j]);
                    m.deng(z[j],z[i]);
                    m.deng(z[i],t);
                }
                
            }
        }
         
    System.out.println(z[0].num+"  "+z[0].world);
         
         
     }

主函数

    public static void main(String[] args) {
        // TODO 自动生成的方法存根
        lian a=new lian();
        a.shuruFile();
        String c;
        c=a.duqufile();
        a.tongjufile(c);
        



    }
    
原文地址:https://www.cnblogs.com/zql98/p/9774992.html