java 计算文件中的字符数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2549817/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Counting the number of characters in a file
提问by Kat
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.
我正在编写一个程序,其中一部分要求程序打印文件中有多少个字符(包括空格)。我现在拥有的代码虽然每次都返回 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;
}
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:
编辑:我的程序规范说我应该使用扫描仪。我不相信它会进入 for 循环,尽管我不确定。当我使用相同的技术来计算文件中的行数时,它工作得很好。那个代码是:
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.
我们不需要检查该行是否包含任何内容。但是,如果它出现在文本文件的中间,则应计为一个字符。
回答by Thilo
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();
回答by leonbloy
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)
你为什么使用扫描仪?一个简单的 Reader 就足够了。如果你坚持使用 Scanner,你必须明白它把它的输入分成由某种模式分隔的字段(默认情况下是一个空格),也许可以设置一个空模式,以便字段与字符对应(但同样,那会矫枉过正,只需使用阅读器)
回答by Stephen Denne
Have you already read everything from the Scanner before passing it to getcharCount()?
在将它传递给之前,您是否已经从扫描仪中读取了所有内容getcharCount()?
回答by mportiz08
I haven't worked with Scanner too much, but wouldn't
我没有过多地使用 Scanner,但不会
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
只是一个想法 - 我不确定这是否会对执行产生任何重大影响
回答by Binary Nerd
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.
另外,您说第二个代码示例运行良好。您是否打印出 line 的值以验证它是否包含任何内容?它可能已经正确计算了行数,但是如果行实际上不包含任何内容,那么这将有助于解释为什么它没有进入您的 for 循环。
回答by Chris Thornton
You need to assign the file, open it, and reset to the beginning. You don't show any of that.
您需要分配文件,打开它,然后重置到开头。你没有表现出来。

