第二周博客作業
阿新 • • 發佈:2018-09-25
src png ima href git return 是否 sta 路徑
本次作業要求參見: 20180918-1 詞頻統計 [https://edu.cnblogs.com/campus/nenu/2018fall/homework/2126]
本次作業代碼地址:https://git.coding.net/FanF/wf.git
1.控制臺輸入英文文本,讀取文本內容。
String content = ""; String con = null;
while(content!=null){ content = bufferedReader.readLine(); if(content == null){ break; }if(content.equals("")){ continue; } count += getWord(content, map); System.out.println("該篇文章總字數為:"+count+"個");
檢測單詞中是否包含特殊字符,若有,則用""空串替代。
public static String checkIsHaveSpecial(String word) { String[] special= {",","(",")","-","*","\"","?",".","—","=","+","","!","/","1","2","3" ,"4","5","6","7","8","9","0",":","$","@"," ","(",")"}; //特殊字符集 for(int i = 0;i<special.length;i++) { if(word.contains(special[i])) { word = word.replace(special[i],""); } } return word;
2.控制臺輸入英文文本,統計單詞和出現次數。調用getWord方法獲取字母並添加到map中,並返回每一行的單詞數,並相加。
public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); String content = ""; String con = null; con = sc.nextLine(); Map map = new TreeMap<String,Integer>(); int count = 0; count = getWord(con, map);
System.out.println("該篇文章總字數為:"+count+"個"); sortMap(map); }
3.選擇文件,輸入文件路徑,讀取文件。
public static void main(String[] args) throws Exception { String f; System.out.println("請輸入文件路徑:"); Scanner sc = new Scanner(System.in); f = sc.nextLine(); File file = new File(f);
4.從控制臺輸入英文單篇作品。
public static void main(String[] args) throws Exception { File file = new File("C:/book.txt"); BufferedReader bufferedReader = new BufferedReader(new FileReader(file),1024*1024*1); String content = ""; Map map = new TreeMap<String,Integer>(); int count = 0; while(content!=null){ content = bufferedReader.readLine(); if(content == null){ break; } if(content.equals("")){ continue; } count += getWord(content, map); } System.out.println("該篇文章總字數為:"+count+"個"); sortMap(map); }
程序做得少,框架來的很慢,在執行中有多次錯誤,很多小問題,花費了很多時間才修正,例如在裏面查找文件夾目錄裏的小說時,沒有註意txt的後綴問題,後來一點點排出才發現。
1.測試功能的實現
此項目PSP
第二周博客作業