zoukankan      html  css  js  c++  java
  • java工程项目作业1

    用户需求:英语的26 个字母的频率在一本小说中是如何分布的?某类型文章中常出现的单词是什么?某作家最常用的词汇是什么?《哈利波特》 中最常用的短语是什么,等等。

    要求:输出单个文件中的前 N 个最常出现的英语单词,并将结果输入到文本文件中。

    我按照老师要求把整个程序分成了三部:

    第一步:文件生成

    import java.io.*;
    public class MyFileOutput {
       public static void main (String argv[]){
          FileInputStream fin;
          FileOutputStream fout;
          int ch;
          try{
               fin=new FileInputStream(FileDescriptor.in);
               fout=new FileOutputStream("zhong.txt");
               System.out.println("请输入一行字符:");
               while((ch=fin.read())!=' ')fout.write(ch);
               fout.close();
               fin.close();

               System.out.println("文件写入成功!");
               }
           catch(FileNotFoundException e){
               System.out.println("不能创建文件");

             }
          catch(IOException e){
               System.out.println("输入流有误!");

                }
          }

    }

    第二步:查单词

    import java.util.*;
    public class CountWord {
       public static String[] strTostrArray(String str) {
            str = str.toLowerCase();// 将字符串中的英文部分的字符全部变为小写
            String regex = "[\W]+";// 非字母的正则表达式 --W:表示任意一个非单词字符
            str = str.replaceAll(regex, " ");
            String[] strs = str.split(" "); // 以空格作为分隔符获得字符串数组
            return strs;
       }
    public static void countword(String[] strs) {
           HashMap<String, Integer> strhash = new HashMap<String, Integer>();
           Integer in = null;// 用于存放put操作的返回值
           for (String s : strs) {// 遍历数组 strs
           in = strhash.put(s, 1);
           if (in != null) {// 判断如果返回的不是null,则+1再放进去就是出现的次数
           strhash.put(s, in + 1);
            }
    }
           Set<java.util.Map.Entry<String, Integer>> entrySet = strhash.entrySet();
           String maxStr = null;// 用于存放出现最多的单词
           int maxValue = 0;// 用于存放出现最多的次数
           for (java.util.Map.Entry<String, Integer> e : entrySet) {
                String key = e.getKey();
                Integer value = e.getValue();
                if (value > maxValue) {
                     maxValue = value;
                     maxStr = key;
    }
    }

    第三步:文件导入

    import java.io.*;
    public class TypeFile {
    public static void main(String args[]){
    FileInputStream fin;
    FileOutputStream fout;
    int ch;
    if(args.length<1){
    System.out.println("请指定文件名");
    return;

    }
    try{
    fin=new FileInputStream(args[0]);
    fout=new FileOutputStream(FileDescriptor.out);
    while((ch=fin.read())!=-1)
    fout.write(ch);
    fin.close();
    fout.close();

    }
    catch(FileNotFoundException e){
    System.out.println("文件没找到");

    }
    catch(IOException e){
    System.out.println("读入文件有误!");
    }
    }

  • 相关阅读:
    iOS开发UI中懒加载的使用方法
    ios archives 出现的是other items而不是iOS Apps的解决方案
    Unable to find a team with the given Team ID或者Failed to code sign的问题解决
    Xcode升级插件失效解决办法-升级版
    iOS 全局禁止横屏,但UIWebView 全屏播放视频,横屏,解决办法
    【iOS进阶】UIWebview加载搜狐视频,自动跳回客户端 问题解决
    Swift学习笔记
    微信分享无响应的解决
    xcode6-添加真机设备
    【iOS系列】-UIWebView加载网页禁止左右滑动
  • 原文地址:https://www.cnblogs.com/gkl20173667/p/9775257.html
Copyright © 2011-2022 走看看