1、成绩统计。

输入文件input.txt:

张三 语文12 数学31 英语11
李四 语文22 数学22 英语22
王五 语文33 数学33 英语33

期待输出output.txt:

张三 语文12 数学31 英语11 总分54
李四 语文22 数学22 英语22 总分66
王五 语文33 数学33 英语33 总分99

代码:

package com.my.test;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern; public class AnalysisResult {
private static Pattern r = Pattern.compile("(\\d+)");
private static int getTotal(String line) {
Matcher m = r.matcher(line);
int total = 0;
while (m.find()) {
total += Integer.valueOf(m.group(0));
}
return total;
} public static void main(String args[]) {
try {
/* 读入TXT文件 */
File readFile = new File("input.txt");
InputStreamReader inputStreamReader = new InputStreamReader(
new FileInputStream(readFile));
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String line;
List<String> contents = new ArrayList<>();
while ((line = bufferedReader.readLine()) != null) {
contents.add(line);
}
bufferedReader.close(); /* 写入TXT文件 */
File writeFile = new File("output.txt");
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(writeFile, true));
PrintWriter pw = new PrintWriter(bufferedWriter);
for (int i = 0; i != contents.size(); ++i) {
String temp = contents.get(i);
pw.println(temp + " 总分" + getTotal(temp));
}
bufferedWriter.flush();
bufferedWriter.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}

/

2、单词统计。

输入文件input.txt:

Today, when I go home after school, I find that I lose my key.
I feel so worried and try to remember all the places I have been to.
As I walk into the shop, an old man stops me and asks if I lose the key.
I feel so excited and say yes. At last, I find my key and go home.
Though I am lucky this time, I tell myself not to do it again.

期待输出:

代码:

package com.my.test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern; public class WordStatistic { private static int total = 0;
private static Pattern r = Pattern.compile("(\\b[a-zA-Z]+\\b)");
private static Map<String, Integer> wordMap = new HashMap<>();
private static void countWords(String line) {
Matcher m = r.matcher(line);
while (m.find()) {
total++;
String word = m.group(0);
Integer wordCount = wordMap.get(word);
if (wordCount != null) {
wordMap.put(word, wordCount + 1);
} else {
wordMap.put(word, 1);
}
}
} private static List sortByValue(Map<String, Integer> map) {
List<Map.Entry<String, Integer>> result = new ArrayList<>(map.entrySet());
result.sort(Comparator.comparing(Map.Entry::getValue));
return result;
} public static void main(String[] args) {
try {
/* 读入TXT文件 */
File readFile = new File("input.txt");
InputStreamReader inputStreamReader = new InputStreamReader(
new FileInputStream(readFile));
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String line;
while ((line = bufferedReader.readLine()) != null) {
countWords(line);
}
bufferedReader.close();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("单词总数:" + total);
System.out.println("不同单词的个数:" + wordMap.size());
List list = sortByValue(wordMap);
for (int i = list.size() - 1; i != 0; --i) {
System.out.println(list.get(i));
}
}
}

最新文章

  1. 算法与数据结构(十五) 归并排序(Swift 3.0版)
  2. 游走 bzoj 3143
  3. C#与Java对比学习:类型判断、类与接口继承、代码规范与编码习惯、常量定义
  4. Cannot add Controls from 64-bit Assemblies to the Toolbox or Use in Designers Within the Visual Studio IDE
  5. 处理程序“ExtensionlessUrlHandler-Integrated-4.0”在其模块列表
  6. Linux学习之路--启动VNC服务
  7. 使用virtualenvwrapper隔离python环境
  8. 动态设置uitableview高度,参考
  9. Codeforces Round #322 (Div. 2) A. Vasya the Hipster 水题
  10. OpenStackCLI调试及术语识记
  11. 一个失败的操作系统MULTICS
  12. Java学习之内部类
  13. android adb经常使用的命令
  14. C/C++迭代器使用具体解释
  15. (Chrome42)Lodop总计页面提示“未安装”要么“请升级”可能的原因和解决方案
  16. C# 通过Bartender模板打印条码,二维码, 文字, 及操作RFID标签等。
  17. POJ-2417-Discrete Logging(BSGS)
  18. spring boot自定义starter
  19. MySQL系列-第一章节:MySQL介绍与安装
  20. Linux系统的shell是什么

热门文章

  1. Orders matters: seq2seq for set 实验
  2. error MSB3073: 命令“copy /y
  3. monit介绍和配置
  4. linux/nginx命令
  5. mybatis12--一级缓存
  6. java-03-动手动脑
  7. vue引入第三方的js文件
  8. C++中类型(理解)
  9. online ddl与pt-osc详解
  10. Django-- KindEditor 富文本编辑器使用