猿问

如何从计数中排除空格?

我必须计算文件“测试.txt”中有多少字母(字符)。我把它记下来了,但它也包括了空间,我不想包括。关于如何做到这一点有什么建议吗?非常感谢。


import java.io.*;

class EnglishAnalysis

{

    public static void main(String[] args)

    {

        try

        {

            FileReader fr = new FileReader("test.txt");

            BufferedReader br = new BufferedReader(fr);

            int count = 0;

            String lineCharacters [];

            String line;

            line = br.readLine();

            while (line != null)

            {

                 lineCharacters = line.split("");

                 for (int i = 0; i < lineCharacters.length; i++)

                 {

                     count ++;

                     line = br.readLine();

                 }

                 System.out.println(count);

             }

             br.close();

        }

        catch (IOException e) {}

    }

}


慕的地6264312
浏览 172回答 5
5回答

蝴蝶不菲

或者(Java 8 +),你可以用 和 方法非常轻松地做到这一点:lines()chars()BufferedReader br = new BufferedReader(fr);long count = br.lines().flatMapToInt(e -> e.codePoints()).filter(e -> !Character.isWhiteSpace(e)).count();这将占用文件的行,将它们平面映射到字符,过滤掉任何空格,然后计算元素Stream<String>String::codePoints

拉莫斯之舞

为什么不在从所有空格中清除每行后添加每行的字符数:&nbsp; &nbsp; while (line != null) {&nbsp; &nbsp; &nbsp; &nbsp; count += line.replaceAll("\\s+", "").length();&nbsp; &nbsp; &nbsp; &nbsp; line = br.readLine();&nbsp; &nbsp; }

小怪兽爱吃肉

也许我错过了一些东西,但我不明白分裂有什么好处。我会这样做:public static void main(String[] args) {&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; FileReader fr = new FileReader("/tmp/test.txt");&nbsp; &nbsp; &nbsp; &nbsp; BufferedReader br = new BufferedReader(fr);&nbsp; &nbsp; &nbsp; &nbsp; int count = 0;&nbsp; &nbsp; &nbsp; &nbsp; String line;&nbsp; &nbsp; &nbsp; &nbsp; line = br.readLine();&nbsp; &nbsp; &nbsp; &nbsp; while (line != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < line.length(); i++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (!Character.isWhitespace(line.charAt(i)))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; count++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; line = br.readLine();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; br.close();&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(count);&nbsp; &nbsp; } catch (IOException ex) {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(ex);&nbsp; &nbsp; }}具体来说,我认为根本不需要修改输入,通过调用split,制作额外的副本,执行查找/替换等。这一切都需要额外的时间和空间。实际上,我看不出有任何理由打扰按行处理文件:public static void main(String[] args) {&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; FileReader fr = new FileReader("/tmp/test.txt");&nbsp; &nbsp; &nbsp; &nbsp; BufferedReader br = new BufferedReader(fr);&nbsp; &nbsp; &nbsp; &nbsp; int count = 0;&nbsp; &nbsp; &nbsp; &nbsp; while (true) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int c = br.read();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (c < 0)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (!Character.isWhitespace(c))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; count++;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; br.close();&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(count);&nbsp; &nbsp; } catch (IOException ex) {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(ex);&nbsp; &nbsp; }}使用缓冲读取器本身可以让您有效地不逐个字符读取文件。因此,通过这种方式可以保存每行的额外副本。

摇曳的蔷薇

字符类具有“是白色空间()”方法java.lang.Character.isWhitespace(字符 ch)while (line != null)&nbsp; {&nbsp; &nbsp; for (int i = 0; i < line.length(); i++)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; if (!Character.isWhitespace(line.charAt(i)))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;count ++;&nbsp; &nbsp; &nbsp; line = br.readLine();&nbsp; &nbsp; }&nbsp; &nbsp; System.out.println(count);&nbsp; }

陪伴而非守候

为什么不在执行拆分和计数之前从字符串中删除空格?一种方法是使用正则表达式:while (line != null){&nbsp; line.replaceAll("\\s+","");&nbsp; lineCharacters = line.split("");&nbsp; ...}
随时随地看视频慕课网APP

相关分类

Java
我要回答