简体   繁体   English

计算文本文件中的字符

[英]Counting characters in text file

I am trying to make a program that reads from a file and counts how many lowercase letters, uppercase letters, whitespace characters (tab, enter etc.) and everything else. 我正在尝试创建一个从文件中读取的程序,并计算多少个小写字母,大写字母,空白字符(制表符,输入等)以及其他所有内容。

This is the code I have and it shows the right amount of letters but for some reason the "other" counter show way more then it should. 这是我的代码,它显示了正确数量的字母,但由于某种原因,“其他”计数器显示的方式应该更多。 Anyone know what I have done wrong? 有人知道我做错了吗?

    File file = new File("C:\\Temp\\Test.txt");
    Scanner s = new Scanner(file).useDelimiter("");

    for (int i = 0; i < file.length(); i++) {
        char c = s.next().charAt(0);

        if (Character.isLetter(c)) {
            if (Character.isLowerCase(c)) {
                isLower++;
            } else {
                isUpper++;
            }
        } else if (Character.isSpaceChar(c) || (c == '\n') || (c == '\t')) {
            isSpace++;
        } else {
            isOther++;
        }

    }

isOther计数器包含\\ r \\ n另外的东西,因为utf8字符乘以2.检查编码并检查文件格式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM