• 单词统计


    通过循环累加查出出现次数最多的单词

    以空格做间隔

    文件导入避免重复输入

    public class test{
    
    /*
    * 读取指定路径下的文件名和目录名
    */
    public void getFileList() throws IOException {
    System.out.println("请输入路径");
    Scanner scan=new Scanner(System.in);
    String a=scan.next();
    File file = new File(a);
    
    File[] fileList = file.listFiles();
    
    for (int i1 = 0; i1 < fileList.length; i1++) {
    if (fileList[i1].isFile()) {
    String fileName = fileList[i1].getName();
    if(fileName.indexOf(".txt")!=-1)
    {
    Word word=new Word(); //单词的链头
    Word lian,xin; 
    String str="";
    String S=a+"\"+fileName;
    System.out.println(S);
    FileReader f=new FileReader(S); 
    char[] c=new char[1]; 
    int b=0;
    boolean exist=false; 
    while((b=f.read(c))!=-1)
    {
    if(String.valueOf(c).equals("
    ")||String.valueOf(c).equals("
    ")||String.valueOf(c).equals(" ")||String.valueOf(c).equals(",")||String.valueOf(c).equals(".")||String.valueOf(c).equals(""")||String.valueOf(c).equals("'"))
    {
    lian=word;
    while(lian!=null) 
    {
    if(lian.value.equalsIgnoreCase(str)) 
    {
    lian.geshu++;exist=true;break;
    }
    else
    {
    lian=lian.next;
    }
    }
    if(exist==false) 
    {
    xin=new Word(str,1);
    xin.next=word.next;
    word.next=xin;
    str="";
    }
    else
    {
    exist=false;
    str="";
    }
    }
    else //单词
    {
    str+=String.valueOf(c);
    }
    }
    
    System.out.println("请输入您想查询的前几个出现此处最多的单词");
    int N=scan.nextInt();
    for(int i=1;i<=N;i++) 
    {
    xin=new Word("",0);
    lian=word.next;
    
    while(lian!=null)
    {
    if(lian.geshu>xin.geshu)
    {
    xin=lian;
    }
    lian=lian.next;
    }
    
    System.out.println("第"+i+"个 :"+xin.value+"个数:"+xin.geshu);
    lian=word;
    
    while(lian.next!=null)
    {
    if(lian.next.value.equalsIgnoreCase(xin.value))
    {
    lian.next=lian.next.next;
    break;
    }
    lian=lian.next;
    }
    }
    }
    System.out.println("文件:" + fileName); 
    }
    
    }
    }
    
    }
    自我抑郁又自我救赎
  • 相关阅读:
    DockerFile构建步骤及命令
    linux安装nginx及常用命令
    docker常用命令
    Docker安装
    获取TrustedInstaller权限
    获取本机公网ip的url地址
    centOS7配置ip
    VS Code配置c语言环境
    Linux l 2.4.20-8 # 溢出
    VMware Destination Host Unreachable
  • 原文地址:https://www.cnblogs.com/zjm15511858030/p/11071155.html
Copyright © 2020-2023  润新知