zoukankan      html  css  js  c++  java
  • java 作业11.4

    package text3;
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import java.util.Arrays;
    import java.util.Scanner;
    public class word3{
    
    public word3(File ads) throws Exception
    { 
    
    File file = new File(ads.toString());//导入txt文档
    String name = file.getName();
    if(!name.trim().toLowerCase().endsWith(".txt")) {
    System.out.println("非.txt文件");
    return;
    }
    if(!file.isFile()) {
    System.out.println("不是文件");
    return;
    }	
    FileReader reader = new FileReader(file);
    int fileLen = (int)file.length();
    char[] chars = new char[fileLen];
    reader.read(chars);
    String txt = String.valueOf(chars);
    String[] a2=txt.split("[^a-zA-Z]+");
    int x=a2.length;
    String[] a = new String[x];
    for(int i=0;i<x;i++)
    {a[i]=a2[i].toLowerCase();
    }
    int n=a.length;
    int kind=0; 
    Object[][] b=new Object[n][2];
    for(int zs=0;zs<n;zs++){
    int k=0;
    for(int i=0;i<kind;i++){
    if(((String) b[i][0]).equalsIgnoreCase(a[zs])){
    b[i][1]=(int)b[i][1]+1;
    k=1;
    break;
    }
    }
    if(k==0){
    b[kind][0]=a[zs];
    b[kind][1]=1;
    kind++;
    } 
    }
    int max=0;
    int p=0,q=0;
    int[] o= new int [kind];
    String[] arr=new String[kind];
    for(int i=0;i<kind;i++){
    for(int j=0;j<kind;j++){
    if((int)b[j][1]>(int)b[max][1])
    {
    max=j;
    o[j]=0;
    }
    else if((int)b[j][1]==(int)b[max][1])
    {
    if(((String) b[j][0]).compareTo((String)b[max][0])>=0)
    {
    max=max;
    }
    else 
    max=j;
    }
    }	
    System.out.println(b[max][0]+"出现次数为:"+b[max][1]);
    b[max][1]=0;	
    }
    }
    }
    
     
    
    package text3;
    
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import java.io.IOException;
    import java.nio.file.FileSystem;
    import java.nio.file.FileSystems;
    import java.nio.file.Path;
    import java.util.Scanner;
    
    public class dgml {
    public static void main(String[] args) throws Exception {
    String path = "F:\xiaoshuo"; 
    File f = new File(path);
    dgml.getFile(f);
    }
    
    
    public static void getFile(File file) throws Exception{ 
    if(file != null){
    File[] f = file.listFiles();	
    if(f != null){
    for(int i=0;i<f.length;i++){
    getFile(f[i]);
    }
    
    }else{
    System.out.println("以下为"+file+"的统计");
    word3 ds1=new word3(file);
    }
    }
    }
    }
    

      

    运行结果:

  • 相关阅读:
    DLUTOJ 1209 字典序和r-子集
    C++ Standard-Library Random Numbers
    后缀数组模板
    UVA 1398 Meteor
    STL Iterators
    hihocoder1187 Divisors
    51nod 子序列的个数(动态规划)
    51nod 最长单增子序列(动态规划)
    51nod 更难的矩阵取数问题(动态规划)
    51nod 多重背包问题(动态规划)
  • 原文地址:https://www.cnblogs.com/dwx8845/p/11808132.html
Copyright © 2011-2022 走看看