import java.io.*; import java.util.*; public class wordcounter { static HashMap hm = new HashMap (); //In der Hashmap werden die Wrter(key) und ihr Index (wert) gespeichert static int sum = 0;//Zum Index erhhen static List a = new LinkedList(); //Speichert die Hufigkeit static void blather(File inputFile) throws IOException{ Scanner sc = new Scanner(inputFile); sc.useDelimiter("[^a-zA-Z]"); String word; while(sc.hasNext()){ word = sc.next(); word = word.toLowerCase(); if(!word.equals("")){ if(!hm.containsKey(word)){ //Falls Wort noch nicht vorhanden hm.put(word, sum);//fge Wort hinzu sum ++;//Erhhe Zhler a.add(0);//Fge neuen Hufigkeit } int x = a.get(hm.get(word)); a.set(hm.get(word), x + 1); //Erhhe Zhler um 1 } } } public static void main(String[] args){ File f = new File("test.txt");//Name der Datei try { blather(f); } catch(IOException e){ System.err.println("Fehler halt");//Falls was schief geht } for (int i = 0; i< hm.size(); i++){//Luft die Hashmap durch int max = 0; for(int j = 0; j< hm.size(); j++){ if(a.get(max) < a.get(j)) max = j;//Bestimme Maximum } System.out.print("Anzahl: "+ a.get(max)+" ");//Gibt Anzahl aus //etwas komplizierte Lsung um vom Wert (max) auf den Schlssel zu kommen String wert; Set test = hm.keySet(); Object[] test2 = test.toArray(); //in test2 stehen jetzt alle Wrter int x = 0; for(; x < test2.length; x++){ if(hm.get(test2[x]) == max){//Falls test2[x] das gesuchte ist breche ab break; } } System.out.println("Wort: "+ test2[x]);//Und gebe es aus a.set(max,0);//Damit nicht zweimal das gleiche Maximum gefunden wird } } }