import java.io.*; import java.util.*; public class HelloWorld{ public static void main(String []args)throws IOException { Scanner sc = new Scanner(new File("test.txt")); String str=""; while(sc.hasNext()) { String s = sc.next(); str+=s; str+=" "; } HashMap<String, Integer> map = new HashMap<>(); String sentence = str.toLowerCase(); for(String word : sentence.split("\\W")) { if(word.isEmpty()) continue; if(map.containsKey(word)) map.put(word, map.get(word)+1); else map.put(word, 1); } for(Map.Entry<String, Integer> entry : map.entrySet()) System.out.println(entry.getKey() + ": " + entry.getValue()); } }
We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy. Accept Learn more