计算文件中的字符数

发布于 2024-08-27 08:24:14 字数 756 浏览 8 评论 0原文

我正在编写一个程序,其中一部分要求程序打印文件中有多少个字符(包括空格)。我现在的代码虽然每次都返回 0,但我不确定为什么它不计算字符。

public int getcharCount(Scanner textFile) {

        int count = 0;

        while(textFile.hasNext()) {
            String line = textFile.nextLine();
            for(int i=0; i < line.length(); i++)
                count++;
        }   
        return count;

    }

编辑:我的程序的规格说明我应该使用扫描仪。尽管我不确定,但我不相信它会进入 for 循环。当我使用相同的技术来计算文件中的行数时,它工作得很好。该代码是:

 public int getLineCount(Scanner textFile) {
    int lineCount = 0;

    while(textFile.hasNext()) {
        String line = textFile.nextLine();
        lineCount++;
    }

    return lineCount;
}

我们不需要检查该行是否包含任何内容。如果它出现在文本文件的中间,则应将其计为一个字符。

I'm writing a program that for one part asks for the program to print how many characters (including whitespaces) are in a file. The code I have right now though returns 0 every time though and I'm not sure why it isn't counting the characters.

public int getcharCount(Scanner textFile) {

        int count = 0;

        while(textFile.hasNext()) {
            String line = textFile.nextLine();
            for(int i=0; i < line.length(); i++)
                count++;
        }   
        return count;

    }

Edit: The specifications for my program say that I should use a scanner. I don't believe it is making it to the for loop though I'm not sure. When I used the same technique to count the number of lines in the file, it worked perfectly. That code was:

 public int getLineCount(Scanner textFile) {
    int lineCount = 0;

    while(textFile.hasNext()) {
        String line = textFile.nextLine();
        lineCount++;
    }

    return lineCount;
}

And we aren't required to check if the line contains anything or not. If it appears in the middle of the text file, it should however be counted as one character.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

粉红×色少女 2024-09-03 08:24:14

我不知道为什么它不起作用(下面的代码不会修复它),但

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

可以写得更简洁为

  count += line.length();

I don't know why it does not work (and the code below will not fix it), but

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

can be written more concise as

  count += line.length();
第七度阳光i 2024-09-03 08:24:14

您为什么使用扫描仪?
一个简单的 Reader 就足够了。
如果您坚持使用扫描仪,您必须了解它将输入划分为由某种模式(默认情况下为空格)分隔的字段,也许可以设置一个空模式,以便字段与字符相对应(但同样,这将太过分了,只需使用阅读器)

Why are you using a Scanner ?
A simple Reader would suffice.
If you insist in using a Scanner, you must understand that it divides its input in fields separated by some pattern (a space by default), perhaps one can set an empty pattern so that the fields correspond with the characters (but again, that would be overkill, just use a Reader)

一梦等七年七年为一梦 2024-09-03 08:24:14

在将扫描仪传递给 getcharCount() 之前,您是否已经读取了扫描仪中的所有内容?

Have you already read everything from the Scanner before passing it to getcharCount()?

假面具 2024-09-03 08:24:14

您需要分配该文件,打开它,然后重置到开头。你没有表现出任何这些。

You need to assign the file, open it, and reset to the beginning. You don't show any of that.

零度° 2024-09-03 08:24:14

您为扫描仪设置什么分隔符?您不会在代码中显示它,但您需要使其将一行视为单个标记。

scanner.useDelimiter("\n");

另外,您说第二个代码示例运行得很好。您是否打印了 line 的值来验证它是否包含任何内容?它可能已经正确计算了行数,但如果行实际上不包含任何内容,那么这将有助于解释为什么它没有进入 for 循环。

What delimiter are you setting for the scanner? You don't show it in your code, but you need to make it treat a line as a single token.

scanner.useDelimiter("\n");

Also, you say that the second code example worked perfectly. Did you print out the value of line to verify that it contained anything? It might have counted the lines properly, but if line didn't actually contain anything then that would help explain why it doesn't enter your for loop.

早茶月光 2024-09-03 08:24:14

我没有太多地使用扫描仪,但不会

textFile.hasNextline();

比只是一个想法更有意义

textFile.hasNext();

——我不确定这是否会对执行产生重大影响

I haven't worked with Scanner too much, but wouldn't

textFile.hasNextline();

make a little more sense than

textFile.hasNext();

Just a thought--I'm not sure if that would have any major effect on the execution

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文