find frequency of each word in a string in java

Java
//import the necessary packages if needed
import java.util.*;
import java.math.*;
@SuppressWarnings("unchecked")//Do not delete this line
public class CountOfWords
{
    public static void main (String[] args) {
        BigInteger a = new BigInteger("1");

    Map<String, BigInteger> countByWords = new HashMap<String, BigInteger>();
    TreeMap<String, BigInteger> sorted = new TreeMap<String, BigInteger>();
    Scanner sc = new Scanner(System.in);
    ArrayList<String> strings = new ArrayList<String>();
        while (sc.hasNext())
            {
                strings.add(sc.next().toLowerCase());
            }
    BigInteger num= new BigInteger("0");
    for(String s: strings) {
        num=num.add(new BigInteger("1"));
        s=s.trim();
        s=s.replace(",","");
        s=s.replace(";","");
        s=s.replace(":","");
        s=s.replace(".","");
        s=s.replace("?","");
        s=s.replace("!","");
        
        BigInteger count = countByWords.get(s);
        if (count != null) {
            countByWords.put(s, count=count.add(new BigInteger("1")));
        } else {
            countByWords.put(s, a);
        }
    }
    sorted.putAll(countByWords);
    System.out.println("Number of words "+num);
    System.out.println("Words with the count");
    for (String name: sorted.keySet()){
            String key = name.toString();
            String value = sorted.get(name).toString();  
            System.out.println(key + ": " + value);  
}
System.out.println(" ");
}
}
Source

Also in Java: