Accounting achieve different words in a file and the number of characters in the two functions, the first row into a text file program, and then to realize the operation of a single character by charAt () function, and the total number of characters with a set of statistics and the number of different characters, each character in turn outputs the number and the percentage of the total. Calculating the number of words is achieved by determining whether a non-word character, using sort () function to achieve ascending operation. Its output word by the number of cycles.
package All; import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Scanner; public class all { public static void main(String[] args)throws IOException//扔掉很重要 { File file = new File("f.txt"); System.out.println("1.统计英文单词个数"+" "+"2.统计字母个数"); Scanner sc = new Scanner(System.in); sc.nextInt value = int (); BufferedReader br = new new BufferedReader (fr); // configured to read a class file BufferedReader HashMap <String, Integer> hm = new HashMap <> () ; // build a new HashMap object, must enforce the HashMap is the String as key, to Integer value. String line = null; switch(value) { . 1 Case: txtString2 (File); BREAK; Case 2: txtString (File); BREAK; } } / * * Statistical letter * / public static void txtString (File File) throws IOException { the try { // the file contents of the IO read operation the FileReader new new fr = the FileReader (File); @SuppressWarnings ( "Resource") Integer COUNT = 0; // number of each letter Integer total = 0; // count the total number of letters, as a percentage char CH; the while ((Line br.readLine = ()) = null) {! for (int J = 0; J <line.length (); J ++) { CH = line.charAt (J); IF ((CH> = 'A' && CH <= 'Z') || (CH> = 'A' && CH < = 'Z')) {// converts a string object to a character array of characters. +. 1 Total = Total; COUNT = hm.get (CH + ""); IF (COUNT == null) { COUNT =. 1; } the else { COUNT ++; } hm.put (CH + "", COUNT); } } / * for (String str: hm.keySet () ) {// str acquired key variable provided LV = List.get (list.size () -. 1-I) .getValue () * 1.0 / Total * 100; the System.out. println (str + "number:" + hm.get (str) + " System.out.println ( "Total number of letters:" Total +); List <of Map.Entry <String, Integer List >> new new = the ArrayList <of Map.Entry <String, Integer >> (hm.entrySet ()); // in java, if you want to object or collection of objects to sort an array, you need to implement Comparator interface to achieve the target we want Comparator <Map.Entry <String, Integer >> comparator = new Comparator <Map. the Entry <String, Integer >> () { public int Compare (of Map.Entry <String, Integer> left, of Map.Entry <String, Integer> right) { return (left.getValue (). the compareTo (right.getValue () )); } }; // default set of ascending ascending the Collections.sort (List, Comparator); Double LV = 0; for (int i = 0; i <52; i ++) {// descending output List.get = LV (list.size () -. 1-I) .getValue () * 1.0 / 100 * Total; System.out.println (List.get (list.size () -. 1-I) .getKey ( .) + ":" + List.get (list.size () -. 1-I) .getValue () + "" + String.format ( "%. 2F", LV) + "%"); } } the catch ( E a FileNotFoundException) { e.printStackTrace (); } } / * * statistical word */ public static void txtString2(File file) throws IOException{ FileReader fr = new FileReader(file); BufferedReader br = new BufferedReader (fr) ; // Construct a class to read the file BufferedReader the StringBuffer the StringBuffer new new SB = (); String Line = null; the while (! (= br.readLine Line ()) = null) { sb.append (Line); // read-out characters are appended to the stringbuffer } fr.close (); // read off the inflow String str = sb.toString () toLowerCase ( );. // Switch to the stringBuffer and lowercase characters String [] words = str.split ( " [^ (a -zA-Z)] + ") ; // non-character segmenting words, all words to obtain the Map <String, Integer> = new new the HashMap Map <String, Integer> (); for (String Word: words) { IF (as map.get (Word) == null) {// If there is first described, then added to the map, the number of occurrences of 1 Map. PUT (Word, 1); } the else { map.put (Word, as map.get (Word) + 1'd); // If present, the accumulated number 1 } } // Sort List <Map.Entry <String, Integer >> list = new ArrayList <Map.Entry <String , Integer >> (map.entrySet ()); Comparator<Map.Entry<String,Integer>> comparator = new Comparator<Map.Entry<String, Integer>>() { public int compare(Map.Entry<String, Integer> left, Map.Entry<String, Integer> right) { int i=left.getValue()-right.getValue(); if(i==0) { return (right.getKey().compareTo(left.getKey())); } return (left.getValue().compareTo(right.getValue())); } }; // default set of ascending the Collections.sort (List, Comparator); int n=list.size(); System.out.println("一共有"+n+"个单词"); for (int I = 0; I <n-; I ++) {// descending output System.out.print (list.get (list.size () . 1--i) .getKey () + ":" + List.get (list.size () -. 1-I) .getValue () + ""); } } }
Screenshot results: