Write a program that parses the frequency of occurrences of each word in a string and displays the word and the frequency at which it appears. (the words are separated by a space, such as "Hello World My First Unit Test");

Source: Internet
Author: User

ImportJava.io.BufferedReader; ImportJava.io.FileReader; Importjava.io.FileNotFoundException; Importjava.io.IOException; ImportJava.util.*;  Public classone{ Public Static voidMain (string[] args) {Try{bufferedreader br=NewBufferedReader (NewFileReader ("D:\\test.txt"));//new buffer Read read for required statistics fileStringBuffer MP=NewStringBuffer ();               String s;            ;  while((S=br.readline ())! =NULL) {mp.append (s); } Map<String,Integer> map =NewHashmap<string, integer> ();//sort by using hash sort methodStringTokenizer st =NewStringTokenizer (Mp.tostring (), ",.! \ n");//Split String//The string used to test if there is a token generator can have more tokens.  and save the divided words in the letter string.               while(St.hasmoretokens ()) {String letter=St.nexttoken (); intcount; if(Map.get (letter) = =NULL) {Count= 1;//indicates that no segmentation has been performed. }Else{Count= Map.get (letter). Intvalue () + 1;               } map.put (Letter,count); } Set<WordEntity> set =NewTreeset<wordentity>();  for(String key:map.keySet ()) {Set.add (Newwordentity (Key,map.get (key))); }                                                         intCount = 1;  for(Iterator<wordentity> it =set.iterator (); It.hasnext ();) {wordentity W=It.next (); if(!w.getkey (). Equals ("the") &&!w.getkey (). Equals ("is") &&!w.getkey (). Equals ("to") &&! W.getkey (). Equals ("by"), &&!w.getkey (). Equals ("is") &&!w.getkey (). Equals ("a") &&!w.getkey ( ). Equals ("and") &&!w.getkey (). Equals ("was") &&!w.getkey (). Equals ("have") &&!w.getkey (). Equals ("had") &&!w.getkey (). Equals ("I") &&!w.getkey (). Equals ("for") &&!w.getkey (). Equals ( "My") &&!w.getkey (). Equals ("Me") &&!w.getkey (). Equals ("with") &&!w.getkey (). Equals ("of") &&!w.getkey (). Equals ("in") &&!w.getkey (). Equals (' on ') &&!w.getkey (). Equals ("that") & &!w.getkey (). Equals ("it") &&!w.getkey (). Equals ("the") &&!w.getkey (). Equals ("at") &&! W.getkey (). Equals ("which") &&!w.getkey (). Equals ("he") &&!w.getkey (). Equals ("as")                           &&!w.getkey (). Equals ("but") &&!w.getkey (). Equals ("he") &&!w.getkey (). Equals ("from") &&!w.getkey (). Equals ("some") &&!w.getkey (). Equals ("Be") &&!w.getkey (). Equals ("were") &&!w.getkey (). Equals ("not") &&!w.getkey (). Equals ("they") &&!w.getkey (). Equals ("this") &&!w.getkey (). Equals ("an") &&!w.getkey (). Equals ("No") &&!w.getkey (). Equals ("into") & &!w.getkey (). Equals ("It") &&!w.getkey (). Equals ("There") &&!w.getkey (). Equals ("but") & &!w.getkey (). Equals ("him") &&!w.getkey (). Equals ("could") &&!w.getkey (). Equals ("Been") & &!w.getkey (). Equals ("would") &&!w.getkey (). Equals ("she") &&!w.getkey (). Equals ("then") & &!w.getkey (). Equals ("then") &&!w.getkey (). Equals ("has") {System.out.println ("+ Count +" named Word: "+ w.getkey () +" occurrences: "+W.getcount ()); if(count = = 10)//when you output 10, you jump out of the loop                      Break; Count++; }                   }           } Catch(FileNotFoundException e) {System.out.println ("File not Found!" ");//Exception Handling}Catch(IOException e) {System.out.println ("File read Abnormal!" ");//Exception Handling         }       }   }  classWordentityImplementsComparable<wordentity> {          PrivateString Key; PrivateInteger Count;  Publicwordentity (String key, Integer count) { This. Key =key;  This. Count =count; }           Public intcompareTo (wordentity o) {intCMP = Count.intvalue ()-O.count.intvalue (); return(CMP = = 0? Key.compareto (o.key):-CMP); //just add a minus sign here to decide whether to sort ascending or descending-cmp in descending order, CMP ascending//because TreeSet will call Workformap's CompareTo method to determine its own sort        }                         PublicString toString () {returnKey + "Number of occurrences:" +count; }                 PublicString GetKey () {returnkey; }                 PublicInteger GetCount () {returncount; }      }         

Write a program that parses the frequency of occurrences of each word in a string and displays the word and the frequency at which it appears. (the words are separated by a space, such as "Hello World My First Unit Test");

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.