1. Create a class that implements the function of counting the number of characters and strings in a text file, and requires implementation of:
A) The number of characters to output by character statistics
b) Output the number of individual words by word count
2. On the basis of B) to implement a class keywordidentifier, read into a Java program source file, output the number of keywords (the note appears in the keyword does not count the number of keywords)
Think: Statistics 1: If the text file contains only 26 English letters and separated by a space, then only the array can be used to count the characters, separated by a space to get the string can count the string (whether case-sensitive problem). If the text file is in English, but contains various punctuation and other symbols, use the form of the collection Key-value to count the characters, read the characters sequentially, and then treat all characters except 26 letters as spaces, then separate them with spaces, and then count the string counts.
Keyword statistics: All keyword collections are required, each line is judged to be a comment, space is separated for non-annotations, and the keyword is counted (in key-value form)
1. Statistical characters and Word Stringcounter.java
ImportJava.util.Map;ImportJava.util.TreeMap; Public classStringcounter {Privatemap<string, integer> charcount =NewTreemap<string, integer>(); Privatemap<string, integer> wordCount =NewTreemap<string, integer>(); Public voidCharcounter (String s) {Integer freq; for(inti = 0; I < s.length (); i++) {freq= Charcount.get (S.substring (i, i+1)); if(Freq = =NULL) {freq=NewInteger (1); } Else{freq=NewInteger (Freq.intvalue () + 1); } charcount.put (S.substring (i, I+1), freq); } } Public voidWordCounter (String s) {//turn other symbols except a-za-z0-9 into spaces, and then convert multiple spaces to a spaceString S1 = S.replaceall ("\\w", "" "); String S2= S1.replaceall ("+", "" "); String[] S3= S2.split (""); Integer freq; for(String str:s3) {freq=wordcount.get (str); if(Freq = =NULL) {freq=NewInteger (1); } Else{freq=NewInteger (Freq.intvalue () + 1); } wordcount.put (str, freq); } } Public voidoutput () {System.out.println (charcount); System.out.println (WordCount); }}
2. Test the text file used as a random English text
Importjava.io.IOException;ImportJava.io.RandomAccessFile; Public classCounttest { Public Static voidMain (string[] args)throwsIOException {Stringcounter sc=NewStringcounter (); LongFilepoint = 0; String s; Randomaccessfile file=NewRandomaccessfile ("String.txt", "R"); LongFilelength =file.length (); while(Filepoint <filelength) {s=File.readline (); //Processing and CountingSc.charcounter (s); Sc.wordcounter (s); Filepoint=File.getfilepointer (); } file.close (); Sc.output (); }}
3. Keyword statistics
First put all the keywords in the map, the keyword is the key, the number of times 0 is the value, and then read the text count
Importjava.io.IOException;ImportJava.io.RandomAccessFile;ImportJava.util.Map;ImportJava.util.TreeMap; Public classKeywordidentifier {Privatemap<string, integer> keywordcount =NewTreemap<string, integer>(); Public voidKeyWord ()throwsIOException {LongFilepoint = 0; String s; Randomaccessfile file=NewRandomaccessfile ("Keyword.txt", "R"); LongFilelength =file.length (); while(Filepoint <filelength) {s=File.readline (); String[] S1= S.split (""); Integer freq=NewInteger (0); for(String word:s1) {keywordcount.put (Word, freq); } filepoint=File.getfilepointer (); } file.close (); } Public voidKeywordcounter (String s) {intpos = S.indexof ("//"); if(pos = =-1) {pos=s.length (); } String Sub= s.substring (0, POS);
String sub1 = Sub.replaceall ("\\w", "" "); String Str= Sub1.replaceall ("+", "" "); String[] S1= Str.split (""); Integer freq; for(String word:s1) {freq=keywordcount.get (word); if(Freq! =NULL) {freq=NewInteger (Freq.intvalue () + 1); Keywordcount.put (Word, freq); } } } Public voidoutput () {System.out.println (keywordcount); } }
4. Test keyword Statistics
ImportJava.io.*; Public classCounttest { Public Static voidMain (string[] args)throwsIOException {Keywordidentifier sc=NewKeywordidentifier (); Sc.keyword (); LongFilepoint = 0; String s; Randomaccessfile file=NewRandomaccessfile ("Counttest.java", "R"); LongFilelength =file.length (); while(Filepoint <filelength) {s=File.readline (); Sc.keywordcounter (s); Filepoint=File.getfilepointer (); } file.close (); Sc.output (); }}
Output:
{abstract=0, assert=0, boolean=0, break=0, byte=0, case=0, catch=0, char=0, class= "1", const=0, Continue=0, default=0, do= 0, Double=0, else=0, enum=0, Extends=0, Final=0, finally=0, float=0, for=0, goto=0, if=0, implements=0, Import=1, instance Of=0, Int=0, interface=0, long=2, native=0, new=2, package=1, private=0, Protected=0, public=2, return=0, short=0, static= 1, Strictfp=0, super=0, switch=0, Synchronized=0, This=0, throw=0, Throws=1, transient=0, try=0, Void=1, volatile=0, while =1}
Text statistics (Java)