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