Job 4 Word Frequency Statistics 2

Source: Internet
Author: User

    • [ must do 2] read small text file a_tale_of_two_cities.txt or large text file gone_with_the_ Wind.txt, statistics the frequency at which a specified word appears in the text file.
    • command-line format: prompt > Myapp.exe-f filename.txt-w Word (ps:c++ program, Java program output similar)
    • Explain:
      • Option-F means opening a file (filename.txt)
      • The-w option indicates how often the word is followed in the Open file (filename.txt).
 Packagefrequency statistics;ImportJava.io.BufferedReader;ImportJava.io.File;Importjava.io.FileNotFoundException;ImportJava.io.FileReader;Importjava.io.IOException;ImportJava.util.Scanner; Public classWord2 { Public Staticstring Fun (String a) {if(A.contains (",") | | A.contains ("."))//The word contains punctuation. {a=a.substring (0,a.length ()-1);//remove the punctuation.                 returnA; }            Else                returnA; }           Public Static voidMain (string []s1) {string S; Charm = 0, N; String []data=NewSTRING[1000];//Deposit WordsString []value=NewSTRING[1000];//words, not repeating.         int[]count=New int[1000];//The corresponding number.         intSum=0, J;  for(i=0;i<1000;i++)//number of initialization. {Count[i]=1; } System.out.println ("Import file input 1, query word input 2"); Scanner M1=NewScanner (system.in); String m2=M1.nextline (); Switch(m2) { Case1:system.out.println ("Please enter the file path and filename you want to query:"); Scanner SC=NewScanner (system.in); String Files=Sc.nextline ();                Sc.close (); File File=NewFile (files); Try{FileReader FileReader=NewFileReader ("n"); BufferedReader BR=NewBufferedReader (FileReader); S=br.readline ();//read the first line                         while(s!=NULL) {String []ss=s.split ("");//Remove the word                             for(i=0;i<ss.length;i++) {Ss[i]=fun (Ss[i]);//remove the punctuation.                                  for(j=0;j<sum;j++)                                {                                    if(Ss[i].equals (Value[j])) {Count[j]++;  Break; }                                }                                if(j==sum) {Value[sum++]=Ss[i]; }} s=br.readline ();//read the next line of the file}} System.out.println ("Import is complete. ");  Case2:system.out.println ("Please enter a word to query:"); Scanner N1=NewScanner (system.in); String N2=N1.nextline ();  for(inti=1;i<count.length;i++)                 {                    if(N2.equals (Value[i])) System.out.println ("The number of occurrences of the term in this document is:" +count[i]+ "); }        }                      }                    }

The code, based on the last job modification, adds a switch statement to make the selection so that the effect is not just the direct-F and-W, but the choice.

Job 4 Word Frequency Statistics 2

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.