有一个text.txt文件,求出此文件中有多少个字母,以及多少个大写字母。希望高手能回答下

解决方案 »

  1.   

    读取这个文件的每一行,然后分别对每一行进行中的字母和大写字母进行统计。统计的方法就很多了,可以用字母的ASCLL码范围来进行统计,也可以用正则表达式来统计,,,,
      

  2.   

    按照 3楼的 思路 写了
    这里只能 统计 每行有多少个字母,以及多少个大写字母你可以参考import java.io.BufferedReader;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    public class HowManyLetters { /**
     * @param args
     */
    public static void main(String[] args) {
    try {
    BufferedReader buf = new BufferedReader(new FileReader(
    "E:\\temp\\aa.txt"));
    String line = "";
    try {
    while ((line = buf.readLine()) != null) {
    count(line);
    }
    } catch (IOException e) {
    e.printStackTrace();
    }
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    } }
    public static void count(String s){
            char c ;
            int letters = 0, DaxieLetters=0;
            for(int i = 0; i < s.length(); i++){
                c = s.charAt(i);            
                if((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
                    letters++;
                if(c >= 'A' && c <= 'Z') 
                 DaxieLetters++;            
            }       
            System.out.println("英文字母有" + letters + "个,大写字母有" + DaxieLetters + "个");
        }}