Calculating frequency of each word in a sentence in java

前端 未结 19 2088
夕颜
夕颜 2020-11-29 10:15

I am writing a very basic java program that calculates frequency of each word in a sentence so far i managed to do this much

import java.io.*;

class Linked         


        
19条回答
  •  旧巷少年郎
    2020-11-29 11:09

    public class TestSplit {
    
        public static void main(String[] args) {
                String input="Find the repeated word which is repeated in this string";
                List output= (List) Arrays.asList(input.split(" "));
    
                for(String str: output) {
                        int occurrences = Collections.frequency(output, str);
                        System.out.println("Occurence of " + str+ " is "+occurrences);
                }
    
                System.out.println(output);
        }
    
    }
    

提交回复
热议问题