计算文件中的字符数
我正在编写一个程序,其中一部分要求程序打印文件中有多少个字符(包括空格)。我现在的代码虽然每次都返回 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我不知道为什么它不起作用(下面的代码不会修复它),但
可以写得更简洁为
I don't know why it does not work (and the code below will not fix it), but
can be written more concise as
您为什么使用扫描仪?
一个简单的 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)
在将扫描仪传递给
getcharCount()
之前,您是否已经读取了扫描仪中的所有内容?Have you already read everything from the Scanner before passing it to
getcharCount()
?您需要分配该文件,打开它,然后重置到开头。你没有表现出任何这些。
You need to assign the file, open it, and reset to the beginning. You don't show any of that.
您为扫描仪设置什么分隔符?您不会在代码中显示它,但您需要使其将一行视为单个标记。
另外,您说第二个代码示例运行得很好。您是否打印了 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.
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.
我没有太多地使用扫描仪,但不会
比只是一个想法更有意义
——我不确定这是否会对执行产生重大影响
I haven't worked with Scanner too much, but wouldn't
make a little more sense than
Just a thought--I'm not sure if that would have any major effect on the execution