java the number of occurrences per character in the statistics file
- 2021-07-10 19:41:02
- OfStack
In this paper, we share the specific code of the number of characters in java statistical files for your reference. The specific contents are as follows
package com.zhu.io;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
public class FileCharacter{
Map<Character,Integer>map=new TreeMap<Character,Integer>();
public FileCharacter(String fileName) throws IOException{
BufferedReader br=new BufferedReader(new FileReader(new File(fileName)));
int x;
while((x=br.read())>0){
Character key=new Character((char)x);
if(map.containsKey(key)){
int count=map.get(key);
map.remove(key);
map.put(key, ++count);
}else{
map.put(key, 1);
}
}
}
public int getCount(char c){ // Gets the number of characters appearing in the file
return map.get(c);
}
public Set<Character> getAllChar(){ // Object for the characters in the file Set Set
return map.keySet();
}
public Map<Character,Integer> getMap(){ // Object consisting of a character and its number of occurrences Map Set
return map;
}
public void printInfo(){ // Print information
Set<Map.Entry<Character, Integer>>set=map.entrySet();
for(Map.Entry<Character, Integer> entry:set){
System.out.println("[ "+entry.getKey()+" ]"+"\t"+"count:"+entry.getValue());
}
}
public static void main(String[] args) throws IOException {
FileCharacter fc=new FileCharacter("e:\\test.txt");
fc.printInfo();
}
}
This site shares another code for everyone: Calculate the number of times each character appears in a string
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
/**
* Calculation 1 Number of occurrences of each character in a string
*
* Train of thought :
* Pass toCharArray() Get 1 Character array -->
* Traversing an array , Use array elements as key, Numerical value 1 As value Deposit map Container -->
* If key Repetition , Pass getKey() Get value, Calculation value+1 Post-deposit
*/
public class Test01 {
public static void main(String[] args) {
System.out.println(" Please enter a string: ");
Scanner sc=new Scanner(System.in);
while (sc.hasNextLine()){
String str=sc.nextLine();
Map<Character,Integer> map =count(str);
System.out.println(map);
}
}
public static Map<Character,Integer> count(String str){
Map<Character,Integer> map=new HashMap<Character,Integer>();
char[] array_char=str.toCharArray();// Convert a string into an array of characters
for(char arr_char: array_char){// Traversing an array of characters
if(map.containsKey(arr_char)){// Check whether the character is in the map Adj. key If it exists in the
Integer old=map.get(arr_char);// Pass key Get value Value of
map.put(arr_char,old+1);// Put characters into map Adj. key In, value Set to pass through key Get value Value of +1
}else{// Check whether the character is in the map Adj. key If it does not exist, put the characters into the map Adj. key In, value The default setting is 1
map.put(arr_char,1);
}
}
return map;
}
}