add Base solution for hw4
This commit is contained in:
@@ -1,7 +1,53 @@
|
|||||||
package wordStat;
|
import java.io.*;
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
public class WordStat {
|
public class WordStat {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
System.out.println("hello world!");
|
if (args.length != 2) {
|
||||||
|
System.err.println("incorrect input!");
|
||||||
|
System.err.println("usage: java WordStat inputFile outputFile");
|
||||||
|
}
|
||||||
|
|
||||||
|
String inputFileName = args[0];
|
||||||
|
String outputFileName = args[1];
|
||||||
|
try {
|
||||||
|
BufferedReader r = new BufferedReader(new FileReader(inputFileName));
|
||||||
|
|
||||||
|
LinkedHashMap<String, Integer> wordCount = new LinkedHashMap<>();
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
|
||||||
|
int data = r.read();
|
||||||
|
while (data != -1) {
|
||||||
|
char c = (char) data;
|
||||||
|
|
||||||
|
if (Character.getType(c) == Character.DASH_PUNCTUATION ||
|
||||||
|
Character.isLetter(c) || c == '\'') {
|
||||||
|
sb.append(c);
|
||||||
|
} else {
|
||||||
|
if (sb.length() > 0) {
|
||||||
|
String word = sb.toString().toLowerCase();
|
||||||
|
wordCount.put(word, wordCount.getOrDefault(word, 0) + 1);
|
||||||
|
sb.setLength(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
data = r.read();
|
||||||
|
}
|
||||||
|
|
||||||
|
r.close();
|
||||||
|
|
||||||
|
PrintWriter writer = new PrintWriter(outputFileName, "UTF-8");
|
||||||
|
|
||||||
|
for (Map.Entry<String, Integer> entry : wordCount.entrySet()) {
|
||||||
|
String key = entry.getKey();
|
||||||
|
int value = entry.getValue();
|
||||||
|
writer.println(key + " " + value);
|
||||||
|
}
|
||||||
|
|
||||||
|
writer.close();
|
||||||
|
|
||||||
|
} catch (Exception ex) {
|
||||||
|
System.err.println("An error occured: " + ex.getMessage());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user