Count the words in English articles

Source: Internet
Author: User

The purpose of this program is to write the words in the text, colleagues of the punctuation to go out, at the beginning I used scanner to input, each string to enter, first not to consider the problem of punctuation, in the place of punctuation will encounter some problems, I want to start by converting to a character array to give up, encountered a lot of problems: the problem of uncertain array length, but also through the Internet access to data know that the Java array is not the same as C, C + + has the terminating character, Java does not have such a terminating character. So in the end I think of Java can intercept substring, through the position of the punctuation mark, and then use substing to extract the substring to be stored in the class array to compare, record.

Here is the original code:

Importjava.io.IOException;ImportJava.io.PrintWriter;Importjava.nio.file.Paths;ImportJava.util.Scanner; Public classScend10_11 { Public Static voidMain (string[] args)throwsIOException {Findword [] word; Word=NewFindword [100]; word[0]=NewFindword (); word[0].input (word); word[0].show (word); word[0].outfile (word); }    }classfindword{PrivateString Word;//Word    Private intNum//number of wordsFindword () {Word=NULL; Num=1; }         PublicString Getword () {returnWord; }     Public voidSetword (String word) { This. Word =Word; }     Public intGetnum () {returnnum; }     Public voidSetnum (intnum) {         This. num =num; }    voidaddnum () {num++; }    voidOutFile (Findword word[])throwsioexception{//output the results to a filePrintWriter out =NewPrintWriter ("WordTimes.txt"); intI=0;  for(i=1;i<100;i++) {            if(word[i]!=NULL) {out.println (Word[i].getword ()+ "\ T" +word[i].getnum ());        }} out.flush ();    Out.close (); }    voidInput (Findword word[])throwsioexception{//file entry and punctuationScanner in =NewScanner (Paths.get ("Word.txt"));        String K;  while(In.hasnextline ()) {String s=In.next ();                                                 Compare (S,word); //The deduction of punctuation and the electrophoresis of the comparison of words and the method of inputting                    }    }    Private voidCompare (String Str1,findword word[]) {intJ=0; Booleanf=false; Chars1[]=Str1.tochararray ();  for(intI=0;i<str1.length (); i++) {            if((s1[i]>=65&&s1[i]<=90) | | (s1[i]>=97&&s1[i]<=122)); Else{j=i;f=true;} }        if(f==true) {            if(J==str1.length ()-1) Change (str1.substring (0, J), Word); Else{Change (str1.substring (0, J), Word); Change (str1.substring (J+1, Str1.length ()), word); }        }        ElseChange (Str1,word);//Call Change    }    Private voidChange (String s, Findword word[]) {//Compare the same words and save        intJ; Booleando{false;  for(j=0;word[j]!=NULL; j + +) {            if(Word[j].getword ()! =NULL&&s.compareto (Word[j].getword ()) ==0) {word[j].addnum (); Bo=true;  Break; } Bo=false; }        if(bo==false) {Word[j]=NewFindword ();            Word[j].setword (s); Word[j].setnum (1); }            }     Public voidShow (findword[] word2) {//Output Results        intI=0;  for(i=1;i<100;i++) {            if(word2[i]!=NULL) {System.out.println (Word2[i].getword ()+ "\ T" +word2[i].getnum ()); }        }            }}

This program I did not sort the final results, this operation will be modified after this program.

Count the words in English articles

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.