Calculating frequency of each word in a sentence in java

前端 未结 19 2131
夕颜
夕颜 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:08

    The following program finds the frequency, sorts it accordingly, and prints it.

    Below is the output grouped by frequency:

    0-10:
           The   2
           Is    4
    11-20:
           Have 13
           Done 15
    

    Here is my program:

    package com.company;
    import java.io.*;
    import java.util.*;
    import java.lang.*;
    
    /**
     * Created by ayush on 12/3/17.
     */
    
    public class Linked {
    
        public static void main(String args[]) throws IOException {
    
            BufferedReader br = new BufferedReader(
                    new InputStreamReader(System.in));
            System.out.println("Enter the sentence");
            String st = br.readLine();
            st=st.trim();
            st = st + " ";
            int count = lengthx(st);
            System.out.println(count);
            String arr[] = new String[count];
            int p = 0;
            int c = 0;
    
            for (int i = 0; i < st.length(); i++) {
                if (st.charAt(i) == ' ') {
                    arr[p] = st.substring(c,i);
                    System.out.println(arr[p]);
                    c = i + 1;
                    p++;
                }
            }
            Map map = new HashMap<>();
    
            for (String w : arr) {
                Integer n = map.get(w);
                n = (n == null) ? 1 : ++n;
                map.put(w, n);
            }
            for (String key : map.keySet()) {
                System.out.println(key + " = " + map.get(key));
            }
    
            Set> entries = map.entrySet();
    
            Comparator> valueComparator = new Comparator>() {
    
                @Override
                public int compare(Map.Entry e1, Map.Entry e2) {
                    Integer v1 = e1.getValue();
                    Integer v2 = e2.getValue();
                    return v1.compareTo(v2); }
            };
    
            List> listOfEntries = new ArrayList>(entries);
            Collections.sort(listOfEntries, valueComparator);
    
            LinkedHashMap sortedByValue = new LinkedHashMap(listOfEntries.size());
    
            for(Map.Entry entry : listOfEntries){
    
                sortedByValue.put(entry.getKey(), entry.getValue());
            }
    
            for(Map.Entry entry : listOfEntries){
    
                sortedByValue.put(entry.getKey(), entry.getValue());
            }
    
            System.out.println("HashMap after sorting entries by values ");
            Set> entrySetSortedByValue = sortedByValue.entrySet();
            for(Map.Entry mapping : entrySetSortedByValue){
                System.out.println(mapping.getKey() + " ==> " + mapping.getValue());
            }
    
    
        }
    
        static int lengthx(String a) {
            int count = 0;
            for (int j = 0; j < a.length(); j++) {
                if (a.charAt(j) == ' ') {
                    count++;
                }
            }
            return count;
        }
    }
    

提交回复
热议问题