注册 登录
编程论坛 JAVA论坛

问题:计算一个文件中的字符数,单词数,行数

bug娃娃 发布于 2019-06-15 16:23, 2149 次点击
我自己的实现如下:
package homeWork6;

import java.util.Scanner;
import *;

public class T0822 {     
    private static BufferedReader reader;

    public static void main(String[] args) throws Exception {
    //统计一个文件中有好多的字符数,单词数,以及行数
    static void q2() throws Exception{
        File file = new File("text2.txt");
               
        FileInputStream file1 = new FileInputStream(file);
        InputStreamReader in = new InputStreamReader(file1);   
        reader = new BufferedReader(in);
        
        String line = null;
        int charCount = 0;
        int wordCount = 0;
        int hangCount = 0;
        
        while((line = reader.readLine()) != null) {           
            if(!line.equals("")) { //一行什么都没有
                //计算字符个数
                charCount += line.length();
               
                //计算单词个数
                String[] str = line.split(" ");
                wordCount += str.length;
               
                //计算行数
                hangCount++;                        
            }                       
        }
        System.out.println("文件中的字符个数为:" + charCount);
        System.out.println("文件中的单词个数为:" + wordCount);
        System.out.println("文件中的行数个数为:" + hangCount);     
    }
}
计算结果的单词数和行数有错误,但是我觉得我的想法没有错
1 回复
#2
bug娃娃2019-06-15 16:24
text2:
When learning Englise ,we have to face the problem of memorizing words.Some students can't remember a word no matter how much time they have spent on it.This is because they don't have a proper way.To my mind,the best way of memorizing words is to memorize the phrases and the sentences related to them.By reciting these phrases and the sentences,we can remember the words easily.What's more,keeping pratice is very important.We should recite the phrases and sentences very often in order to learn them by heart.
1