本次作业要求参见: 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
原文:https://www.cnblogs.com/fjingxuan/p/9696884.html