Java利用Map實(shí)現(xiàn)計(jì)算文本中字符個數(shù)
一、題目要求
有一個英文的文本文檔a.txt , 需要讀取該文本文檔并計(jì)算文檔中英文字符出現(xiàn)的次數(shù),最后打印輸出
注意:map集合只能放入英文字符,不能夠有空格數(shù)字引號等字符,所以在寫代碼的時候需要額外的進(jìn)行判斷,還需要考慮的一點(diǎn)是英文字符是有大小寫的,但是統(tǒng)計(jì)在map里面就不區(qū)分大小寫
二、分析
1、需要先獲取文檔的路徑,并創(chuàng)建HashMap作為存放次數(shù)的容器,key的類型為Character存放字符,value的類型為Integer存放次數(shù)
2、通過文檔輸入流讀取文檔中的內(nèi)容,得到一串字符串
3、遍歷字符串,獲得字符串中每一個字符
4、判斷字符在map集合中是否存在
5、不存在則將其放入
6、存在則將map集合中字符key對應(yīng)的次數(shù)value取出,然后加一,再通過put方法重新放回,起到更新次數(shù)的效果
三、部分代碼展示
1、創(chuàng)建Map列表及文件路徑
FileInputStream fis = null; HashMap<Character, Integer> map = new HashMap<>();
2、獲得文本中的內(nèi)容并返回為字符串
while ((len = fis.read(b))!=-1){
// 獲得文件中的字符串
s = new String(b, 0, len);
}
3、遍歷字符串并獲得每個字符再進(jìn)行相關(guān)判斷后放入map集合中
// 遍歷字符串得到每一個字符
for (int i = 0; i < s.length() ; i++) {
char c = s.charAt(i);
// 判斷是否是字母
if ( ((c>='a')&&(c<='z')) || ((c>='A')&&(c<='Z'))){
// 將字母轉(zhuǎn)換為小寫
char nc = Character.toLowerCase(c);
// 判斷map集合中是否有該鍵
if (map.containsKey(nc)){
Integer num = map.get(nc);
num++;
map.put(nc,num);
}else {
map.put(nc,1);
}
}
}
4、遍歷map集合得到數(shù)據(jù)
// 遍歷map集合得到數(shù)據(jù)
Set<Character> key = map.keySet();
for (Character c : key) {
Integer value = map.get(c);
System.out.println("key="+c+"----"+"value="+value);
}四、全部代碼
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class totalNum {
public static void main(String[] args) throws IOException {
// todo 統(tǒng)計(jì)字母出現(xiàn)的次數(shù)
FileInputStream fis = null;
String s = null;
HashMap<Character, Integer> map = new HashMap<>();
try {
fis = new FileInputStream("E:\\JavaCode\\JavaSE\\Day8-25\\src\\a1\\a.txt");
byte[] b = new byte[1024];
int len = 0;
while ((len = fis.read(b))!=-1){
// 獲得文件中的字符串
s = new String(b, 0, len);
}
// 遍歷字符串得到每一個字符
for (int i = 0; i < s.length() ; i++) {
char c = s.charAt(i);
// 判斷是否是字母
if ( ((c>='a')&&(c<='z')) || ((c>='A')&&(c<='Z'))){
// 將字母轉(zhuǎn)換為小寫
char nc = Character.toLowerCase(c);
// 判斷map集合中是否有該鍵
if (map.containsKey(nc)){
Integer num = map.get(nc);
num++;
map.put(nc,num);
}else {
map.put(nc,1);
}
}
}
// 遍歷map集合得到數(shù)據(jù)
Set<Character> key = map.keySet();
for (Character c : key) {
Integer value = map.get(c);
System.out.println("key="+c+"----"+"value="+value);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
fis.close();
}
}
}五、結(jié)果截圖(部分)

六、a.txt文本
I am happy to join with you. today in what will go down in history ,as the greatest demonstration for freedom in the history of our nation…
Five score years ago, a great American, in whose symbolic shadow we stand today, signed the Emancipation Proclamation. This momentous decree came as a great beacon light of hope to millions of Negro slaves who had been seared in the flames of withering injustice. It came as a joyous daybreak to end the long night of bad captivity.
到此這篇關(guān)于Java利用Map實(shí)現(xiàn)計(jì)算文本中字符個數(shù)的文章就介紹到這了,更多相關(guān)Java計(jì)算文本字符個數(shù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot和前端聯(lián)動實(shí)現(xiàn)存儲瀏覽記錄功能
這篇文章主要介紹了SpringBoot和前端聯(lián)動實(shí)現(xiàn)存儲瀏覽記錄功能,本文通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2024-01-01
java 中JFinal getModel方法和數(shù)據(jù)庫使用出現(xiàn)問題解決辦法
這篇文章主要介紹了java 中JFinal getModel方法和數(shù)據(jù)庫使用出現(xiàn)問題解決辦法的相關(guān)資料,需要的朋友可以參考下2017-04-04
SpringBoot配置文件bootstrap和application區(qū)別及說明
這篇文章主要介紹了SpringBoot配置文件bootstrap和application區(qū)別及說明,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-06-06
通過java反射機(jī)制動態(tài)調(diào)用某方法的總結(jié)(推薦)
下面小編就為大家?guī)硪黄ㄟ^java反射機(jī)制動態(tài)調(diào)用某方法的總結(jié)(推薦)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-07-07

