Repository navigation
Expand file tree
/
Copy pathWordStatCountMiddleL.java
More file actions
68 lines (63 loc) · 2.69 KB
/
Copy pathWordStatCountMiddleL.java
File metadata and controls
68 lines (63 loc) · 2.69 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
import java.io.*;
import java.util.*;
public class WordStatCountMiddleL {
static boolean partOfWrord(char partOfLine) {
if ((partOfLine == '\'') || (Character.DASH_PUNCTUATION == Character.getType(partOfLine))
|| (Character.isLetter(partOfLine))) {
return true;
} else {
return false;
}
}
public static void main(String[] args) {
String fileNameInput = args[0];
String fileNameOutput = args[1];
try (BufferedReader input = new BufferedReader(
new InputStreamReader(new FileInputStream(fileNameInput), "UTF-8"));
BufferedWriter outputFile = new BufferedWriter(
new OutputStreamWriter(new FileOutputStream(fileNameOutput), "UTF-8"));) {
HashMap<String, Integer> output = new HashMap<>();
String keys = "";
while (input.ready()) {
String line = input.readLine().toLowerCase() + " ";
int i = 0;
String word = "";
while (i < line.length()) {
if (partOfWrord(line.charAt(i))) {
word += line.charAt(i);
} else {
if (word.length() > 4) {
String sliceWord = word.substring(2, word.length() - 2);
if ((output.containsKey(sliceWord))) {
output.put(sliceWord, output.get(sliceWord) + 1);
} else {
output.put(sliceWord, 1);
keys = keys + sliceWord + " ";
}
}
word = "";
}
i++;
}
}
keys.strip();
String[] listKeys = keys.split(" ");
Arrays.sort(listKeys, 0, listKeys.length, new Comparator<String>() {
public int compare(String s1, String s2) {
return output.get(s1) - output.get(s2);
}
});
if (keys.length() != 0) {
for (int j = 0; j < listKeys.length; j++) {
outputFile.write(listKeys[j] + " " + Integer.toString(output.get(listKeys[j])) + "\n");
}
}
} catch (FileNotFoundException e) {
System.err.println("Error File Not found" + e.getMessage());
} catch (UnsupportedEncodingException e) {
System.err.println("Error Unsupported Encoding Exception" + e.getMessage());
} catch (IOException e) {
System.err.println("Error IOE" + e.getMessage());
}
}
}