Wordcount作业
一、Github地址:https://github.com/twohamburgers/untitled
二、解题思路
读取指定文件信息,对信息进行筛选,对要统计的数据进行计算,根据不同的输入指令返回不同的数据,先完成基本需求如果有能力再拓展高级需求。
三、设计实现过程
找到Scanner类读取输入数据,FileReader类读取文件信息。
函数有主函数main(),readfile(),got1()在一个类中。
readfile()读取一个文件并统计各类数据存储在哈希表中返回,got1()对输入信息分析并返回需要的数据,主函数控制程序是否结束或者继续使用。
四、代码说明
public static void main(String args[]) { String ad; System.out.println("请输入指令和参数"); do{ ad = got1(); System.out.println("请输入指令和参数"); }while (!ad.equals("-q")); }//-q退出程序
public static String got1(){ Scanner input=new Scanner(System.in); String ip[] = input.nextLine().split(" "); switch (ip[0]){ case "-c":if(readFile(ip[1]).get("Row")!=null) System.out.println("该文件的行数为"+readFile(ip[1]).get("Row")); break; case "-l":if(readFile(ip[1]).get("Chars")!=null) System.out.println("该文件的字符数为"+readFile(ip[1]).get("Chars")); break; case "-w":if(readFile(ip[1]).get("Word")!=null) System.out.println("该文件的单词数为"+readFile(ip[1]).get("Word")); break; default: System.out.println("输入错误"); break;} return ip[0];}
根据不同参数输出不同结果
public static Map<String ,Integer> readFile(String filename) { String pathname = filename; Map<String, Integer> Information = new HashMap<String, Integer>(); int row=0,chars=0,word=0; File file=new File(pathname); if (!file.exists()) { System.out.println("该文件不存在");//文件不存在 }else{; try (FileReader reader = new FileReader(pathname); BufferedReader br = new BufferedReader(reader) ) { String line; while ((line = br.readLine()) != null) {if(line.isEmpty()){row++;} //空白行检测 else{if((line.charAt(0)>='a'&&line.charAt(0)<='z')||(line.charAt(0)>='A'&&line.charAt(0)<='Z')){//单词检测补充 word++; } for(int i=1; i< line.length();i++,chars++) { char c = line.charAt(i); char b=line.charAt(i-1); if(((c>='a'&&c<='z')||(c>='A'&&c<='Z'))&&!((b>='a'&&b<='z')||(b>='A'&&b<='Z'))){//单词检测 word++; } } row++;}} Information.put("Row",row); Information.put("Chars",chars); Information.put("Word",word); } catch (IOException e) { e.printStackTrace(); }} return Information;}
将可能需要的数据全部查询存储在哈希表中返回,需要输出的取出,感觉每个数据写一个方法有点繁琐。
五、测试运行


1.txt为空白文件




六、PSP表格
|
PSP2.1 |
Personal Software Process Stages |
预估耗时(分钟) |
实际耗时(分钟) |
|
Planning |
计划 |
10 |
5 |
|
· Estimate |
· 估计这个任务需要多少时间 |
10 |
5 |
|
Development |
开发 |
65 |
110 |
|
· Analysis |
· 需求分析 (包括学习新技术) |
10 |
15 |
|
· Design Spec |
· 生成设计文档 |
|
|
|
· Design Review |
· 设计复审 (和同事审核设计文档) |
|
|
|
· Coding Standard |
· 代码规范 (为目前的开发制定合适的规范) |
|
|
|
· Design |
· 具体设计 |
10 |
10 |
|
· Coding |
· 具体编码 |
30 |
40 |
|
· Code Review |
· 代码复审 |
5 |
5 |
|
· Test |
· 测试(自我测试,修改代码,提交修改) |
20 |
40 |
|
Reporting |
报告 |
30 |
40 |
|
· Test Report |
· 测试报告 |
20 |
30 |
|
· Size Measurement |
· 计算工作量 |
|
|
|
· Postmortem & Process Improvement Plan |
· 事后总结, 并提出过程改进计划 |
10 |
10 |
|
合计 |
|
七、项目小结
本来以为完成作业很快,实际上手才发现完成一个项目很费时。不仅是编码,其它的工作也很重要而且很费时,测试代码时无法涵盖所有的方面,从而有很多BUG慢慢才发现,这是最耗时的地方,还有和我业务能力方面的不足有关系。收获是对项目开发的流程有了系统的认识,对各种工具的使用更加熟练。希望从这第一次开始积累经验,大概了解开发项目的流程,在以后的学习工作中不断改进,增强个人能力,做到事半功倍的效果。
浙公网安备 33010602011771号