java 实现wordcount_java实现WordCount

该程序使用Java实现,用于统计文本的字符数、单词总数和行数。通过读取控制台参数,可以指定输入文件并选择输出到文件。程序包含四个主要方法:计算字符数、单词数、总行数和代码行/注释行/空行数。测试用例覆盖了所有功能,并提供了Git的使用体验。
摘要由CSDN通过智能技术生成

一、Gitee地址:

二、解题思路:

1、该程序要求在控制台输入参数后运行,所以编程时将输入的参数存入args[]中进行逐个判断。

2、作业要求对指定文本内的字符数、单词总数和行数进行统计,且可指定输出至文件,以及一些其它扩展内容。

3、将文本内容读取进String中,对该字符串进行检测则可得出结果。

4、统计单词个数时,与停用词表中的单词逐个比较,若相同则不计数。

5、注释行、空行、代码行的判断通过正则表达式判断。

三、程序的设计实现过程:

public static String getC(String fileName)    // 计算指定文件中字符的个数

public static String getW(String fileName)   // 计算指定文件中单词的个数

public static String getl(String fileName)     //计算指定文件中的总的行数

public static String geta(String fileName)    //返回具体的代码行数, 注释行数和空的行数

public static void writeFile(String fileName)   // 将结果写入指定的文件中

四、代码说明:

//计算指定文件中字符的个数,

public staticString getC(String fileName) {int count = 0;

String s;try{

BufferedReader in= new BufferedReader(newFileReader(fileName));//FileReader in = new FileReader(fileName);

while ((s = in.readLine()) != null)

count+=s.length();

in.close();

}catch(FileNotFoundException e1) {

System.out.println("找不到文件!!!");

e1.printStackTrace();

}catch(IOException e) {

e.printStackTrace();

}return fileName + ", 字符数为:" +count;

}

//计算指定文件中单词的个数

public staticString getW(String fileName) {int count = 0;

String s;try{

BufferedReader in= new BufferedReader(newFileReader(fileName));while ((s = in.readLine()) != null) {if (!s.isEmpty()) {

String ss[]= s.trim().split(" |,| ");

count+=ss.length;

}

}

in.close();

}catch(FileNotFoundException e1) {

System.out.println("找不到文件!!!");

e1.printStackTrace();

}catch(IOException e) {

e.printStackTrace();

}return fileName + ", 单词数为:" +count;

}

//计算指定文件中的总的行数

public staticString getl(String fileName) {int count = 0;try{

BufferedReader in= new BufferedReader(newFileReader(fileName));while ((in.readLine()) != null)++count;

in.close();

}catch(FileNotFoundException e1) {

System.out.println("找不到文件!!!");

e1.printStackTrace();

}catch(IOException e) {

e.printStackTrace();

}return fileName + ", 总的行数为:" +count;

}

//返回具体的代码行数, 注释行数和空的行数

public staticString geta(String fileName) {int count = 0, space = 0, h = 0;

String s;try{

BufferedReader in= new BufferedReader(newFileReader(fileName));while ((s = in.readLine()) != null) {if (s.isEmpty() || s.trim().length() <= 1)++space;else{int t = 0;while (t < s.length() && s.charAt(t) == ' ')++t;if ((t + 1 < s.length() && s.charAt(t) == '/' && s.charAt(t + 1) == '/')|| (t + 2 < s.length() && s.charAt(t + 1) == '/' && s.charAt(t + 2) == '/'))++h;else

++count;

}

}

in.close();

}catch(FileNotFoundException e1) {

System.out.println("找不到文件!!!");

e1.printStackTrace();

}catch(IOException e) {

e.printStackTrace();

}return fileName + ", 代码行/空行/注释行:" + count + "/" + space + "/" +h;

}

//将结果写入指定的文件中

public static voidwriteFile(String fileName) {try{

FileWriter w= newFileWriter(fileName);int i = 0;for (; i < arrayList.size(); i++) {

w.write(arrayList.get(i));

w.write("\r\n");

}

w.close();

}catch(IOException e) {

System.out.println("文件写入错误");

e.printStackTrace();

}

}

//主函数

public static voidmain(String[] args) {if (args.length > 1) {int last = args.length - 1;

String name= args[last];

String outputName = "";

String tmp;

int flag = 0;//判断是否需要将信息输出到一个文件中

for (int i = 0; i < last; i++)if (args[i].equals("-o")) {

flag= 1;

name= args[last - 2];

outputName=args[last];break;

}//读取命令行中传入的参数

for (int i = 0; i < last; i++)if (args[i].equals("-c")) {

tmp= newString(getC(name));

System.out.println(tmp);

arrayList.add(tmp);

}else if (args[i].equals("-w")) {

tmp= newString(getW(name));

System.out.println(tmp);

arrayList.add(tmp);

}else if (args[i].equals("-l")) {

tmp= newString(getl(name));

System.out.println(tmp);

arrayList.add(tmp);

}else if (args[i].equals("-a")) {

tmp= newString(geta(name));

System.out.println(tmp);

arrayList.add(tmp);

}//将结果输出到一个文件中

if (flag == 1) {

writeFile(outputName);

System.out.println();

System.out.println("将信息写入" + outputName + "文件成功!!!");

}

}elseSystem.out.println("请先输入参数!!!");

}

五、测试用例设计:

测试字符个数::   wc.exe -c test.txt -o result.txt

74c1671c51bcd005f403abb3f680c4e4.png

测试单词个数:   wc.exe -w test.txt -o result.txt

ad1a2409df401f19ee0a6137e3327b9e.png

测试总行数:   wc.exe -l test.txt -o result.txt

b0bdfba20d4ccfbf4d0f407654585a38.png

测试代码行数, 注释行数和空的行数:   wc.exe -a test.txt -o result.txt

ac4ff8a8ab1644b88ede16a695006d2c.png

所有功能测试:  wc.exe -c -w -l -a test.txt -o result.txt

91f62a698dd6afe9a6b231cf30f7bb3b.png

六、总结:

在编码实现的过程中遇到了很多问题,好在网上有很多参考代码,然后自己根据参考代码写了出来。通过这次作业学会了Git的使用。

参考文献链接

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值