Counting the number of characters in a file
Posted
by Kat
on Stack Overflow
See other posts from Stack Overflow
or by Kat
Published on 2010-03-31T01:18:15Z
Indexed on
2010/03/31
1:23 UTC
Read the original article
Hit count: 347
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;
}
© Stack Overflow or respective owner