A simple method to count the number of words and various punctuation marks in a file

Source: Internet
Author: User
This applet uses the most basic method to count the number of Chinese and English words in the text. The idea is also relatively simple: (1) Reading content from the text, use the bufferedreader class to read a row each time and add it to the stringbuffer type variable. The final stringbuffer type variable is text content, such as stringbuffer Sb; (2) converts all Sb content to lowercase letters (or uppercase letters). (3) The number of various punctuation marks in the Statistical File. (4) All punctuation marks are replaced with one, for example, replace the text with commas (5) and use the string segmentation function to obtain the length of the returned string array. This length is the total number of words in the file.
The Code is as follows;
Import Java. io. *; public class wordscount {public static void main (string [] ARGs) throws ioexception {// todo auto-generated method stub bufferedreader BR = new bufferedreader (New filereader ("E: /a.txt "); stringbuffer sb = new stringbuffer (); string line; while (line = BR. readline ())! = NULL) {sb. append (line); // append each row of data read to sb. append ("\ n"); // Add a linefeed to each row to read} system. out. print ("original file content: \ n" + Sb); // print the current article, that is, the string Sb content BR. close (); // close the file stream string S = sb. tostring (). tolowercase (); // converts sb to lowercase and copies it to the string s system. out. print ("after the letter is converted to lowercase file content: \ n" + S); // print the content of the current article, that is, the content of string S, int countch = 0; // number of characters in the statistics file stringbuffer c = new stringbuffer (); // number of characters in the storage file for (INT I = 0; I <S. length (); I ++) {char CH = S. charat (I); // obtain each character in the file and Assign the CH if (! (CH> = 'A' & Ch <= 'Z') {C. append (CH); // append the obtained characters to countch ++;} int Space = 0, Qm = 0, comma = 0, excl = 0, other = 0; // question mark comma exclamation point for (INT I = 0; I <C. length (); I ++) if (C. charat (I) = '') Space ++; else if (C. charat (I) = '? ') QM ++; else if (C. charat (I) = '! ') Excl ++; else if (C. charat (I) = ',') comma ++; else other ++; string temp = ""; if (countch> = 0) {/* (1) replace all punctuation marks with the content of the file after the comma to the temporary string temp. Suppose this file only has ,?! And so on. (2) The replacement can change the actual punctuation. Pay attention to the last two replications !! First, considering that the acronyms, such as warm-hearted, can be replaced with any of the 26 lower-case letters to ensure that the acronyms are recorded as one word. Second, the acronyms, replace 'with a separator. Note the use of escape characters to ensure that the acronym is recorded as a word */temp = S. trim (). replace ('',','). replace ('. ',','). replace ('! ',', '). Replace ('? ',','). Replace ('(',','). replace (')',','). replace ('-', 'A '). replace ('\ '',', '); system. out. println ("\ n with the file content after the comma: \ n" + temp + "\ n"); string [] DATA = temp. split (","); system. out. println ("Total number of spaces:" + space + "Total number of question marks:" + QM + "Total number of exclamation points:" + excl + "Total number of commas: "+ comma +" Other punctuations: "+ other); // print the total number of spaces. system. out. println ("\ n total number of words:" + data. length );}}}

Running result:

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.