[Java小题狂做]文件的简单读入 *** 作

[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);
                }
            }
        }
    }
}

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

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/727641.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-26
下一篇 2022-04-26

发表评论

登录后才能评论

评论列表(0条)

保存