-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWordCount.java
More file actions
executable file
·34 lines (29 loc) · 897 Bytes
/
Copy pathWordCount.java
File metadata and controls
executable file
·34 lines (29 loc) · 897 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import java.util.ArrayList;
import java.util.List;
public class WordCount extends FileProcessor<List<Integer>> {
private int charCount, wordCount, lineCount;
protected void startFile() {
charCount = 0;
wordCount = 0;
lineCount = 0;
}
protected void processLine(String line) {
charCount += line.length();
wordCount += wordsInLine(line);
lineCount++;
}
private int wordsInLine(String line) {
String trim = line.trim();
if (trim.isEmpty()){
return 0;
}
return trim.split("\\s+").length;
}
protected List<Integer> endFile() {
List<Integer> charWordLines = new ArrayList<Integer>();
charWordLines.add(charCount);
charWordLines.add(wordCount);
charWordLines.add(lineCount);
return charWordLines;
}
}