| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1816 人关注过本帖
标题:问题:计算一个文件中的字符数,单词数,行数
只看楼主 加入收藏
bug娃娃
Rank: 2
等 级:论坛游民
帖 子:101
专家分:27
注 册:2019-3-15
结帖率:95.24%
收藏
 问题点数:0 回复次数:1 
问题:计算一个文件中的字符数,单词数,行数
我自己的实现如下:
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);     
    }
}
计算结果的单词数和行数有错误,但是我觉得我的想法没有错
搜索更多相关主题的帖子: 计算 文件 字符 单词 行数 
2019-06-15 16:23
bug娃娃
Rank: 2
等 级:论坛游民
帖 子:101
专家分:27
注 册:2019-3-15
收藏
得分:0 
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.
2019-06-15 16:24
快速回复:问题:计算一个文件中的字符数,单词数,行数
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015095 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved