Skip to content

Instantly share code, notes, and snippets.

@thetekst
Created October 3, 2012 06:15
Show Gist options
  • Save thetekst/3825362 to your computer and use it in GitHub Desktop.
Save thetekst/3825362 to your computer and use it in GitHub Desktop.
CountWords
import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
import static java.lang.System.out;
public class CountWords {
public static String deleteChar(String str){
int count = 0;
char[] getCh = {',', '.', '"', '(', ')'};
for(char element : getCh){
while(str.charAt(str.length()-1) == element) {
++count;
element = 0;
}
}
str = str.substring(0, str.length()-count);
return str;
}
public static void main(String[] args) {
final String PATH = "e:\\job\\task.txt";
// final String PATH = "E:\\JavaSctipt\\bot.js";
String str1 = "";
String str2 = "";
final Map<String, Integer> map = new HashMap<String, Integer>();
final Set<Map.Entry<String, Integer>> set = map.entrySet();
try {
Scanner in = new Scanner(new File(PATH));
while(in.hasNext()){
str1 = in.next();
str1 = deleteChar(str1);
map.put(str1, 0);
}
in.close();
Scanner inE = new Scanner(new File(PATH));
while(inE.hasNext()){
str2 = inE.next();
str2 = deleteChar(str2);
for(Map.Entry<String, Integer> outMap : set) {
if(str2.equals(outMap.getKey())){
int oldValue = outMap.getValue();
map.put(str2, oldValue + 1);
}
}
}
inE.close();
for(Map.Entry<String, Integer> outMap : set) {
// if(outMap.getValue().equals(2)){ // вывыдим слова с 2мя повторениями
out.print(outMap.getKey() + ": ");
out.println(outMap.getValue());
// }
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment