forked from jjfiv/CSC262P1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWordCount.java
41 lines (35 loc) · 972 Bytes
/
WordCount.java
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
35
36
37
38
39
40
41
package edu.smith.cs.csc262.coopsh.apps;
import edu.smith.cs.csc262.coopsh.InputLine;
import edu.smith.cs.csc262.coopsh.ShellEnvironment;
import edu.smith.cs.csc262.coopsh.Task;
public class WordCount extends Task {
int wordCount = 0;
int lineCount = 0;
int byteCount = 0;
public WordCount(ShellEnvironment env, String[] args) {
super(env, args);
}
@Override
protected void update() {
InputLine line = this.input.poll();
if (line == null) {
// still waiting for more...
return;
}
// only output and print when we've seen the whole file!
if (line.isEndOfFile()) {
this.println("lines:" + lineCount);
this.println("words:" + wordCount);
this.println("bytes:" + byteCount);
this.closeOutput();
this.exit(0);
return;
}
// Otherwise, increment this count!
wordCount += line.get().split("\\s+").length;
// increment number of lines
lineCount ++;
//increase number of bytes
byteCount+=line.get().length();
}
}