One,
Output frequency 26 English letters appear in a text file, from high to low, and the percentage of occurrences of the letter display, accurate to two decimal point.
1 package test; 2 3 import java.io.*; 4 5 public class Word { 6 7 public static void read() { 8 String string =null; 9 10 int a[]=new int[52]; 11 char b[]=new char[52]; 12 b[0]='a'; 13 b[1]='b'; 14 b[2]='c'; 15 b[3]='d'; 16 b[4]='e'; 17 b[5]='f'; 18 b[6]='g'; 19 b[7]='h'; 20 b[8]='i'; 21 b[9]='j'; 22 b[10]='k'; 23 b[11]='l'; 24 b[12]='m'; 25 b[13]='n'; 26 b[14]='o'; 27 b[15]='p'; 28 b[16]='q'; 29 b[17]='r'; 30 b[18]='s'; 31 b[19]='t'; 32 b[20]='u'; 33 b[21]='v'; 34 b[22]='w'; 35 b[23]='x'; 36 b[24]='y'; 37 b[25]='z'; 38 b[26]='A'; 39 b[27]='B'; 40 b[28]='C'; 41 b[29]='D'; 42 b[30]='E'; 43 b[31]='F'; 44 b[32]='G'; 45 b[33]='H'; 46 b[34]='I'; 47 b[35]='J'; 48 b[36]='K'; 49 b[37]='L'; 50 b[38]='M'; 51 b[39]='N'; 52 b[40]='O'; 53 b[41]='P'; 54 b[42]='Q'; 55 b[43]='R'; 56 is B [44 is] = 'S' ; 57 is B [45] = 'T' ; 58 B [46 is] = 'the U-' ; 59 B [47] = 'V' ; 60 B [48] = 'W is' ; 61 is B [49] = 'X-' ; 62 is B [50] = 'the Y' ; 63 is B [51 is] = 'the Z' ; 64 65 66 the try { 67 // given data is read from the file name next create a new FileReader 68 FileReader fr = new new FileReader ( "D: \\ QQ file Harry Potter and at The Sorcerer \\'s Stone.txt" ); 69 70 // create a default size of the input buffer buffering character input stream 71 is the BufferedReader br = new new the BufferedReader (fr); 72 73 is the while ( null ! = (String = br.readLine ())) { 74 int SUM = String.length (); 75 for ( int I = 0; I <SUM; I ++ ) 76 { 77 for ( int J = 0; J <52 is; J ++ ) 78 { 79 IF (String.charAt (I) == B [J] ) { 80 A [J] ++ ; 81 } 82 } 83 } 84 } 85 fr.close(); 86 br.close(); 87 } catch (Exception ee) { 88 ee.printStackTrace(); 89 } 90 91 int max=0; 92 double s=0; 93 for(int i=0;i<52;i++) 94 { 95 s+=a[i]; 96 } 97 System.out.println("总次数"+s); 98 for(int j=0;j<52;j++) 99 { 100 int c=0; 101 for(int i=0;i<52;i++) 102 { 103 104 if(a[i]>max) 105 { 106 max=a[i]; 107 c=i; 108 } 109 if(i==51) 110 { 111 System.out.print(b[c]+" 次数 "+a[c]+" 出现率 "); 112 System.out.println(String.format("%.2f", a[c]/s*100)+"%"); 113 a[c]=0; 114 c=0; 115 max=0; 116 } 117 } 118 } 119 } 120 121 public static void main(String[] args) { 122 read(); 123 } 124 }
Second, the first N English words most frequently occurring output a single file. Role: one for the English word count in text files appear console application frequency; words: start with an English letter, a string of letters and alphanumeric symbols regarded as one word. In a word delimiter divided case-insensitive. On output, all the words are lowercase characters.
1 package test; 2 3 import java.io.*; 4 import java.io.FileNotFoundException; 5 import java.io.IOException; 6 import java.util.*; 7 import java.util.Map.Entry; 8 9 public class Test { 10 public static void main(String[] args) { 11 12 13 Scanner sca=new Scanner(System.in); 14 int n; 15 n=sca.nextInt(); 16 long t1 = System.currentTimeMillis(); 17 String s; 18 String fileName1 = "D:\\QQ文件\\Harry Potter and the Sorcerer's Stone.txt"; 19 BufferedReader br = null; 20 try { 21 br = new BufferedReader(new FileReader(fileName1)); 22 StringBuffer sb = new StringBuffer(); 23 //将文件内容存入StringBuffer中 24 while((s = br.readLine()) != null) { 25 sb.append (S); 26 is } 27 // case insensitive 28 String STR = sb.toString () the toLowerCase ();. 29 // partition string and stored in the array (in order, separated by spaces.) 30 String [ ] = str.split Elements ( "[, \\ S]." ); 31 is int COUNT = 0 ; 32 the Map <String, Integer> = myTreeMap new new the TreeMap <String, Integer> (); 33 is // iterate to stored Map <String, Integer> in 34 is for ( int I = 0; I <elements.length; I ++ ) { 35 IF(myTreeMap.containsKey(elements[i])) { 36 count = myTreeMap.get(elements[i]); 37 myTreeMap.put(elements[i], count + 1); 38 } 39 else { 40 myTreeMap.put(elements[i], 1); 41 } 42 } 43 System.out.println("单词统计的结果如下"); 44 //将map.entrySet()转换成list 45 List<Map.Entry<String, Integer>> list = new ArrayList<Map.Entry<String, Integer>>(myTreeMap.entrySet ()); 46 is // implements sorting by a comparator 47 the Collections.sort (List, new new Comparator <of Map.Entry <String, Integer >> () { 48 // Sort Descending 49 public int Compare (the Entry < String, Integer> O1, the Entry <String, Integer> O2) { 50 return o2.getValue () the compareTo (o1.getValue (.)); 51 is } 52 is }); 53 is int NUM =. 1 ; 54 is for (of Map.Entry <String, Integer> Map: List) { 55 IF (NUM <= n-) { 56 is System.out.println ( "number of occurrences of" + num + "word is:" + map.getKey () + " , an occurrence frequency of" + map.getValue () + "views" ); 57 is NUM ++ ; 58 } 59 the else BREAK ; 60 } 61 is } the catch (a FileNotFoundException E) { 62 is e.printStackTrace (); 63 is } the catch (IOException E) { 64 e.printStackTrace (); 65 } the finally { 66 the try { 67 br.close () ; 68 } catch (IOException e) { 69 e.printStackTrace(); 70 } 71 } 72 } 73 }
Third, Requirement 3: English word most commonly occurring before N outputs in a single file. Function 1: the output file will not be repeated for all the words, according to the number of occurrences arranged from more to less, as many times, appear to lexicographic order. Function 2: the specified file directory, the operation performed on a function of each file in the directory 1. Function 3: Specify the file directory, but recursively traverse all subdirectories, each file functions to perform exercise 1.
1 package test; 2 3 import java.io.*; 4 import java.io.FileNotFoundException; 5 import java.io.IOException; 6 import java.util.*; 7 import java.util.Map.Entry; 8 9 public class Test { 10 public static void main(String[] args) { 11 12 13 Scanner sca=new Scanner(System.in); 14 int n; 15 n=sca.nextInt(); 16 long t1 = System.currentTimeMillis(); 17 String s; 18 String fileName1 = "D:\\QQ文件\\Harry Potter and the Sorcerer's Stone.txt"; 19 BufferedReader br = null; 20 try { 21 br = new BufferedReader(new FileReader(fileName1)); 22 StringBuffer sb = new StringBuffer(); 23 //将文件内容存入StringBuffer中 24 while((s = br.readLine()) != null) { 25 sb.append (S); 26 is } 27 // case insensitive 28 String STR = sb.toString () the toLowerCase ();. 29 // partition string and stored in the array (in order, separated by spaces.) 30 String [ ] = str.split Elements ( "[, \\ S]." ); 31 is int COUNT = 0 ; 32 the Map <String, Integer> = myTreeMap new new the TreeMap <String, Integer> (); 33 is // iterate to stored Map <String, Integer> in 34 is for ( int I = 0; I <elements.length; I ++ ) { 35 IF(myTreeMap.containsKey(elements[i])) { 36 count = myTreeMap.get(elements[i]); 37 myTreeMap.put(elements[i], count + 1); 38 } 39 else { 40 myTreeMap.put(elements[i], 1); 41 } 42 } 43 System.out.println("单词统计的结果如下"); 44 //将map.entrySet()转换成list 45 List<Map.Entry<String, Integer>> list = new ArrayList<Map.Entry<String, Integer>>(myTreeMap.entrySet ()); 46 is // implements sorting by a comparator 47 the Collections.sort (List, new new Comparator <of Map.Entry <String, Integer >> () { 48 // Sort Descending 49 public int Compare (the Entry < String, Integer> O1, the Entry <String, Integer> O2) { 50 return o2.getValue () the compareTo (o1.getValue (.)); 51 is } 52 is }); 53 is int NUM =. 1 ; 54 is for (of Map.Entry <String, Integer> Map: List) { 55 IF (NUM <= n-) { 56 is IF (. Map.getKey () contentEquals ( ",") == to false ) 57 is { 58 + + NUM "appears on the number of" System.out.println ( "word is:" + map.getKey () + " , an occurrence frequency of "+ map.getValue () +" views " ); 59 } 60 NUM ++ ; 61 is } 62 is the else BREAK ; 63 is } 64 } the catch (a FileNotFoundException E) { 65 e.printStackTrace (); 66 } the catch (IOException E) { 67 e.printStackTrace(); 68 }finally{ 69 try { 70 br.close(); 71 } catch (IOException e) { 72 e.printStackTrace(); 73 } 74 } 75 } 76 }