1. github: https://github.com/HanKiSei/WordCounter
2. PSP表格
psp2.1 | Personal Software Process Stages |
预估耗时(分钟) | 实际耗时(分钟) |
Planning | 计划 | 45 | 50 |
Estimate | 估计这个任务所需时间 | 45 | 50 |
Development | 开发 | 640 | 520 |
Analysis | 需求分析(学习新技术) | 200 | 150 |
Design spec | 生成设计文档 | 10 | 10 |
Design Review | 设计复审 | 30 | 15 |
Coding standard | 代码规范(为目前开发制定合适的规范) | 10 | 10 |
Design | 具体设计 | 30 | 25 |
Coding | 具体编码 | 100 | 120 |
Code Review | 代码复审 | 60 | 40 |
Test | 测试(自我测试,修改代码,提交修改) | 200 | 150 |
Reporting | 报告 | 100 | 90 |
Test Report | 测试报告 | 20 | 30 |
Size Measurement | 计算工作量 | 20 | 30 |
Postmortem & Process Improvement Plan | 事后总结,并提出过程修改计划 | 60 | 30 |
合计 | 785 | 600 |
3. 解题思路
使用正则表达式匹配单个字符、一个单词、一行
4. 实现过程
import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.util.regex.Matcher; import java.util.regex.Pattern; public class MyWordCounter { public static void main(String[] args) { System.out.println(new File(args[args.length - 1]).getAbsolutePath());; try (BufferedReader br = new BufferedReader(new FileReader(args[args.length - 1]));) { StringBuilder sb = new StringBuilder(""); String buf = null; while ((buf = br.readLine()) != null) { sb.append(buf + ' '); } Pattern p = null; switch (args[0]) { case "-c": p = Pattern.compile("\w");//匹配单个字符 break; case "-w": p = Pattern.compile("\w+");//匹配一个单词 break; case "-l": p = Pattern.compile(" ");//匹配一行 break; } Matcher m = p.matcher(sb); int cnt = 0; while (m.find()) { ++cnt; } switch (args[0]) { case "-c": System.out.println("char:"+cnt); break; case "-w": System.out.println("word:"+cnt); break; case "-l": System.out.println("line:"+cnt); break; } } catch (Exception e) { System.out.println("请在控制台输入参数"); e.printStackTrace(); } } }
5. 测试运行
6.项目小结
通过使用正则表达式,可以更好地查找字符串