Test1.java
package class_five; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.InputStreamReader; import java.text.DecimalFormat; public class Test1 { int j=0; int ci=0; double sum=0; double a1; static char zimu[] = new char[26];//存储字母‘a-z’ static char shu[] = new char[2000];//存储单个字母内容 double count[]=new double[26]; public static void main(String[] args) { Test1 t1=new Test1(); t1.test("D:\\Test\\a.txt"); t1.display(); } public void test(String pathname) { Test1 t1=new Test1(); try { File filename=new File(pathname); InputStreamReader reader=new InputStreamReader(new FileInputStream(filename)); BufferedReader br=new BufferedReader(reader); String line[]=new String[100];; for(int i=0;i<line.length;i++) { line[i]=br.readLine(); } br.close(); int k=0; while(line[k]!=null) { for(int i=0;i<line[k].length();i++) { Shu [J] = Line [K] .charAt (I); J ++ ; } K ++ ; } / * initialize an array of letters * / t1.reLetter (); / * sequence of letters is determined and counted * / for ( int I = 0; I <shu.length; I ++ ) { Switch (Shu [I]) { Case 'A' | 'A': COUNT [0] ++; BREAK ; Case 'b'|'B':count[1]++;break; case 'c'|'C':count[2]++;break; case 'd'|'D':count[3]++;break; case 'e'|'E':count[4]++;break; case 'f'|'F':count[5]++;break; case 'g'|'G':count[6]++;break; case 'h'|'H':count[7]++;break; case 'i'|'I':count[8]++;break; case 'j'|'J':count[9]++;break; case 'k'|'K':count[10]++;break; case 'l'|'L':count[11]++;break; case 'm'|'M':count[12]++;break; case 'n'|'N':count[13]++;break; case 'o'|'O':count[14]++;break; case 'p'|'P':count[15]++;break; case 'q'|'Q':count[16]++;break; case 'r'|'R':count[17]++;break; case 's'|'S':count[18]++;break; case 't'|'T':count[19]++;break; case 'u'|'U':count[20]++;break; case 'v'|'V':count[21]++;break; case 'w'|'W':count[22]++;break; case 'x'|'X':count[23]++;break; case 'y'|'Y':count[24]++;break; case'Z' | 'the Z': COUNT [25] ++; BREAK ; } } } the catch (Exception E) { e.printStackTrace (); } } public void the display () { DecimalFormat DF = new new DecimalFormat ( "0.00" ) ; System.out.println ( "the respective letters appear in the passage statistics as follows:" ); / * find the total number of letters * / for ( int I = 0; I <26 is; I ++ ) { SUM + = COUNT [I]; } / * Frequency in descending order, bubbling method, the same frequency comparator occurs sorted in alphabetical order * / for ( int X = 0; X <26-1; X ++ ) { for ( int Y = X +. 1; Y <26 is; Y ++ ) { IF (COUNT [X]> COUNT [Y]) { Double TEMP = COUNT [X]; COUNT [X] = COUNT [Y]; COUNT [Y] = TEMP; char temp1 = zimu [X]; zimu [X] = zimu [Y]; zimu [Y] = temp1; } } } / * Cycle frequency of occurrence of the letter * / for ( int I = 0; I <= 25; I ++ ) { CI ++ ; A1 = COUNT [I] / * SUM 100 ; System.out.println (CI + " the letter "+ zimu [i] +" appearance frequency is: "+ df.format (A1) +"% " ); } / * display the total number of letters * / System.out.println ( " total "+ ( int ) sum + "letters" ); } / * initialize an array of letters * / public void reLetter () { for (int i = 0; i <26; i ++ ) { zimu [ 0] = 'a' ; zimu [ 1] = 'b' ; zimu [ 2] = 'c' ; zimu [ 3] = 'd' ; zimu [ 4] = 'e' ; zimu [ 5] = 'f' ; zimu [ 6] = 'g' ; zimu [ 7] = 'h' ; zimu [ 8] = 'on' ; zimu [ 9] = 'j' ; zimu [ 10] = 'k' ;; zimu [ 12] = 'm' ; zimu [ 13] = 'n' ; zimu [ 14] = 'or' ; zimu [ 15] = 'p' ; zimu [ 16] = 'q' ; zimu [ 17] = 'r' ; zimu [ 18] = 's' ; zimu [ 19] = 'T' ; zimu [ 20] = 'a' ; zimu [ 21] = 'v' ; zimu [ 22] = 'w' ; zimu [ 23] = 'x' ; Winter [ 25] = 'on' ; } } }
Test2.java
package class_five; import class_third_copy.Test1; import java.io.BufferedWriter; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; public class Test2 extends Test1{ static String wd=""; static String le[]=new String[1000]; static String wd2[]=new String[1000]; static String wd3[]=new new String [1000 ]; static String WD4 [] = new new String [1000 ]; public static void main (String [] args) { Test2 T2 = new new Test2 (); // t1.test ( "D: \\ the Test \ \ a.txt "); // t1.display (); // t2.test2 (); t2.test3 (" D: \\ \\ input1.txt the Test " ); / * method using a stream writing buffer String // BufferedWriter, new new BufferedWriter, BW = (new new FileWriter ( "D: \\ \\ input1.txt the Test")); // bw.flush (); // bw.write(new String));//或者bw.append(new String(buf)); // bw.newLine (); // write a line feed, if necessary // bw.close (); * / / * two methods using the PrintWriter print () method Series * / // PrintWriter PW = null; / / the try { // PW = the PrintWriter new new (new new BufferedWriter, (new new FileWriter ( "D: \\ \\ output1.txt the Test"))); // } the catch (IOException E) { // // the TODO the catch Auto-Generated Block // e.printStackTrace (); // } // pw.print (WD4); // pw.close (); } / * extract word * / public void test2(String path) { Test1 t1=new Test1(); int count1=0; try { t1.test(path); for(int i=0;i<Test1.shu.length;i++) { if((Test1.shu[i]>='a'&&Test1.shu[i]<='z')||(Test1.shu[i]>='A'&&Test1.shu[i]<='Z')) { wd+=Test1.shu[i]; continue; }else { wd2[count1]=wd; wd=""; if(wd2[count1]!=""||wd2[count1]!=null) { count1++; } } } for(int i=0;i<count1;i++) { } }catch(Exception e) { e.printStackTrace(); } } /*去掉重复的单词*/ public void test3(String path) { Test1 t1=new Test1(); int count1=0; try { t1.test(path); ) {for(int i=0;i<Test1.shu.length;i++) { if((Test1.shu[i]>='a'&&Test1.shu[i]<='z')||(Test1.shu[i]>='A'&&Test1.shu[i]<='Z')) { wd+=Test1.shu[i]; continue; }else if(wd.length()!=1){ wd2[count1]=wd; wd=""; if(wd2[count1]!=""|| WD2 [CNT1] == ++ CNT1null; } }else { wd=""; } } /* int count2=0; for(int i=0;i<count1;i++) { for(int j=0;j<count1;j++) { if(j!=i) { if(wd2[i].equals(wd2[j])==true) { break; } } if(j==count1-1) { wd3[count2]=wd2[i]; System.out.println(wd2[i]); System.out.println(wd3[count2]); count2++; } } } for(int i=0;i<count1;i++) { for(int j=0;j<count1;j++) { if(j!=i) { if(wd2[i].equals(wd2[j])==true) { wd3[count2]=wd2[i]; count2++; break; } } } } */ for(int i=0;i<count1;i++) { System.out.println(wd2[i]); } int num1=1; String str=wd2[0]; wd4[0]=str; for(int i=1;i<count1;i++) { char c=str.charAt(str.length()-1); if(c>='A' && c<='Z'){ c+=32; } String str1= String.valueOf(c); System.out.println(); for(int j=i+1;j<count1;j++) { char d=wd2[j].charAt(0); if(d>='A' && d<='Z'){ d+=32; } String str2= String.valueOf(d); if(str1.equals(str2)) { str=wd2[j]; wd4[num1]=wd2[j]; num1++; break; } } } for(int i=0;i<num1;i++) { System.out.println(wd4[i]); } /*int message_a=0; int num_3=1; wd3[0]=wd2[0]; for(int i=0;i<count1;i++) { System.out.println(num_3); for(int j=i+1;j<num_3;j++) { if(wd2[i].equals(wd3[j])==true) { break; } if(j==num_3-1) { message_a=1; } } if(message_a==1) { message_a=0; wd3[num_3+1]=wd2[i]; num_3++; } } for(int i=0;i<num_3;i++) { System.out.println(wd3[4]); }*/ }catch(Exception e) { e.printStackTrace(); } } }