-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextStatistics.java
More file actions
29 lines (25 loc) · 926 Bytes
/
TextStatistics.java
File metadata and controls
29 lines (25 loc) · 926 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
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class TextStatistics {
public static void main(String[] args) {
String filepath = "C:/Users/jesli/OneDrive/Desktop/Java-Lab/Demo0.txt";
try(BufferedReader reader = new BufferedReader(new FileReader(filepath))){
String line;
int wordCount = 0;
int lineCount = 0;
int charCount = 0;
while((line = reader.readLine()) != null ){
lineCount++;
charCount += line.length();
wordCount += line.split("\\s+").length;
}
System.out.println("Character count = " + charCount);
System.out.println("Word count = " + wordCount);
System.out.println("Line count = " + lineCount);
}
catch(IOException e){
System.err.println(e);
}
}
}