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));
}
}
}