Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the best way to determine the total number of words of a file in Java?

What is the best way to find the total number of words in a text file in Java? I'm thinking Perl is the best on finding things such as this. If this is true then calling a Perl function from within Java would be the best? What would you have done in condition such as this? Any better ideas?

like image 947
ashokgelal Avatar asked Nov 28 '22 19:11

ashokgelal


1 Answers

int count = 0;
Scanner sc = new Scanner(new File("my-text-file.txt")); 
while (sc.hasNext()) {
   ++count;
   sc.next();
}
like image 58
Itay Maman Avatar answered Dec 05 '22 10:12

Itay Maman