Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

help using scanner class to count chars in file

807588Apr 17 2009 — edited Apr 18 2009
Hi all, I am learning Java right now and ran into a problem. I need to use the Scanner class to accurately count the number of chars in a file including the line feed chars 10&13.

This what I have and it currently reports a 201 byte file as having 194 chars:

File file = new File(plainFile);
Scanner inputFile = new Scanner(file);

numberOfChars = 0;
String line;

//count characters in file
while (inputFile.hasNextLine())
{
line = inputFile.nextLine();
numberOfChars += line.length();
}


I know there are other ways using BufferedReader, etc but I have to use Scanner class to do this.

Thanks in advance!
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 16 2009
Added on Apr 17 2009
18 comments
385 views