-
Notifications
You must be signed in to change notification settings - Fork 0
/
wc_br.java
executable file
·29 lines (27 loc) · 1.08 KB
/
wc_br.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
import java.io.*;
import java.nio.file.*;
class wc_br {
public static void main(String[] args) throws IOException {
int lineCount = 0, wordCount = 0, charCount = 0, charVal;
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(Files.newInputStream(Paths.get(args[0]))))) {
boolean prevWhitespace = true;
while ((charVal = reader.read()) >= 0) {
charCount++;
if (charVal == '\n') {
lineCount++;
prevWhitespace = true;
} else if (isWhitespace(charVal)) {
prevWhitespace = true;
} else if (prevWhitespace) {
wordCount++;
prevWhitespace = false;
}
}
}
System.out.println(lineCount + " " + wordCount + " " + charCount + " " + args[0]);
}
private static boolean isWhitespace(int charVal) {
return charVal == ' ' || charVal == '\t' || charVal == '\r' || charVal == '\f';
}
}