Java word count program

后端 未结 22 977
北荒
北荒 2020-12-09 06:48

I am trying to make a program on word count which I have partially made and it is giving the correct result but the moment I enter space or more than one space in the string

22条回答
  •  醉梦人生
    2020-12-09 07:16

    try this

    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    public class wordcount {
        public static void main(String[] args) {
            String s = "India is my country. I love India";
            List qw = new ArrayList();
            Map mmm = new HashMap();
            for (String sp : s.split(" ")) {
                qw.add(sp);
            }
            for (String num : qw) {
                mmm.put(num, Collections.frequency(qw, num));
            }
            System.out.println(mmm);
    
        }
    
    }
    

提交回复
热议问题