-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathFileStats.java
More file actions
87 lines (73 loc) · 3.11 KB
/
FileStats.java
File metadata and controls
87 lines (73 loc) · 3.11 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*
* File: FileStats.java
* ----------------------------
* დაწერეთ პროგრამა რომელიც ფაილიდან წაიკითხავს ტექტს და დათვლის შემდეგ
* სტატისტიკებს: სიმბოლოების რაოდენობა, სიტყვების რაოდენობა(სიტყვები სფეისებით
* გამოყოფილია ერთმანეთისგან), წინადადებების რაოდენობა. რამდენი წინდადება მთავრდება
* წერტილით, კითხვის ნიშნით და ძახილის ნიშნით?
*/
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.StringTokenizer;
import acm.program.ConsoleProgram;
public class FileStats extends ConsoleProgram {
public void run() {
// Read Content
String content = readContent("FileStats.java");
// Check If Error
if (content == null)
return;
// Evaluate
int symbolCount = countSymbols(content);
int wordCount = countWords(content);
int sentenceCount = countSentences(content);
int sentencesWithDot = countSentences(content, ".");
int sentencesWithQuestionMark = countSentences(content, "?");
int sentencesWithExclamationPoint = countSentences(content, "!");
// Print Results
println("Total Symbols: " + symbolCount);
println("Total Words: " + wordCount);
println("Total Sentences: " + sentenceCount);
println("Total Sentences Ending with . : " + sentencesWithDot);
println("Total Sentences Ending with ? : " + sentencesWithQuestionMark);
println("Total Sentences Ending with ! : " + sentencesWithExclamationPoint);
}
// Read content and write the whole file in one string.
private String readContent(String fileName) {
try {
String content = "";
BufferedReader br = new BufferedReader(new FileReader(fileName));
while (true) {
String line = br.readLine(); // 1. read current line
if (line == null) { // 2. if (line == null) => there are no more lines to read
break;
}
// 3. at this point (line != null) => there are more lines to read
content += line + '\n'; // 4. append content current line
}
br.close();
return content;
} catch (Exception e) {
println(e);
}
return null;
}
// Count symbols of content
private int countSymbols(String content) {
return content.length();
}
// Count words by StringTokenizer.
private int countWords(String content) {
StringTokenizer st = new StringTokenizer(content);
return st.countTokens();
}
// Count sentences by calling overloaded function
private int countSentences(String content) {
return countSentences(content, ".?!");
}
// Count sentences depends on marks. Mark is symbol at the end of sentence.
private int countSentences(String content, String marks) {
StringTokenizer st = new StringTokenizer(content, marks);
return st.countTokens() - 1;
}
}