[Java小题狂做]第三弹-文件的简单读入操作

这是我们学校的实验作业,大家随缘看看哈
我也当个学习记录了,这个答案的方法和我的方法还是有好有坏的

题目1:统计文件中的信息

题目1
我的答案如下:

package Experiment_3_file_processing;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
/**
 * @author Gremmie102
 * @date 2022/4/24 15:48
 * @purpose :统计文本中出现的字数,行数,字符数(不计空格),和文件所有单词的平均长度
 */
public class TM1_MyWordsCount {
    public static void main(String[] args) throws FileNotFoundException {
        Scanner scanner1 = new Scanner(System.in);
        System.out.println("What file do you want me to examine?");
        String fileName = scanner1.next();
        Scanner scanner2 = new Scanner(new File(fileName));
        examine(scanner2);
        scanner1.close();
        scanner2.close();
    }
    public static void examine(Scanner scanner){
        int lines=0;
        int words=0;
        int chars=0;
        double wordLength=0;
        while(scanner.hasNextLine()){
            String str = scanner.nextLine();
            char[] charArray = str.toCharArray();
            words += str.split(" ").length;
            for (int i=0;i<charArray.length;i++){
                if (charArray[i]!=' '){
                    chars++;
                }
            }
            lines++;
        }
        wordLength = chars/words;
        System.out.println("Total lines = "+lines);
        System.out.println("Total words = "+words);
        System.out.println("Total chars = "+chars);
        System.out.println("Word length = "+wordLength);
    }
}

老师给的答案如下:

package Experiment_3_file_processing;
import java.io.*;
import java.util.*;
/**
 * Created with IntelliJ IDEA.
 * Description: Hello,I would appreciate your comments~
 * User:
 * Date: -04-14
 * Destination:
 */
public class TitleStatistics2 {

    public static void main(String[] args)
            throws FileNotFoundException{
        System.out.println("This program statistics about a file.");
        System.out.println();
        Scanner console = new Scanner(System.in);
        System.out.println("What file do you want me to examine?");
        String filename=console.nextLine();
        Scanner infile=new Scanner(new File(filename),"UTF-8");
        examineFile(infile);
        console.close();

    }
    public static void examineFile(Scanner infile) {
        int lines =0;
        int words=0;
        int chars =0;
        while(infile.hasNextLine()) {
            String line = infile.nextLine();
            lines++;
            Scanner data=new Scanner(line);
            while(data.hasNext()) {
                String word=data.next();
                words++;
                chars+=word.length();
            }
        }
        System.out.println("Total lines ="+ lines);
        System.out.println("Total words = "+words);
        System.out.println("Total chars ="+chars);
        System.out.println("Word length ="+(double)chars/words);
    }
}


题目二

题目二
在这里插入图片描述
在这里插入图片描述
我的答案👇

package Experiment_3_file_processing;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

/**
 * @author Gremmie102
 * @date 2022/4/24 16:09
 * @purpose : 通过文件读入一个人的名字在一个年代的流行指数
 */
public class TM2_NamePopulation {
    public static void main(String[] args) throws FileNotFoundException {
        Scanner scanner1 = new Scanner(System.in);
        System.out.println("输入你的文件名或完整路径");
        String fileName = scanner1.next();
        Scanner scanner2 = new Scanner(new File(fileName));
        System.out.println("请输入你要查找的名字");
        String name = scanner1.next();
        searchNamePopulation(scanner2,name);
        scanner1.close();
        scanner2.close();
    }
    public static void searchNamePopulation(Scanner scanner,String name){
            String line = scanner.nextLine();
            String[] message = line.split(" ");
            if (message[0]!=name){
                System.out.println("\""+name+"\" "+"not found");
            }else{
                for (int i =1;i< message.length;i++){
                    System.out.println("    "+(1900+(i-1)*10)+" "+message[i]);
                }
            }
    }
}

老师给的答案👇

package Experiment_3_file_processing;

import java.io.*;
import java.util.*;
public class population {

    public static void main(String[] args)throws FileNotFoundException {
        Scanner console=new Scanner(System.in);
        System.out.println("name?");
//      先用户输入姓名
        Scanner babyName=new Scanner(new File("E:\\Text.txt"));
//      从文件中读取姓名
        judge(console,babyName);
//      判别文件中第一行的姓名与用户输入的姓名是否一致
        babyName.close();
//      清空scanner流
    }

    public static void judge(Scanner console,Scanner babyName) {
        String names=console.nextLine();
//        用name接受console的流,console置为空
        while(babyName.hasNextLine()) {
            String data=babyName.nextLine();
            Scanner again=new Scanner(data);
            if(again.next().equalsIgnoreCase(names)) {
                while(again.hasNextInt()) {
                    int num=again.nextInt();
                    System.out.println(num);
                }
            }
        }
    }
}

没有难点,就是挺烦的,哈哈哈哈
感谢阅读~

  • 14
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 11
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Gremmie2003

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值