【问题描述】
编写一个文本文件单词统计的程序,包括建立文件、单词统计、单词查询、单词定位的功能。
【基本要求】
程序应先询问用户的 ID号(ID 号包括两个大写字母和4 位数字),
例如: 请输入用户 ID 号:AB1234 程序应对输入的 ID 号验证,符合 ID 号要求的格式,然后程序提示四种选择:
(1) 建立文件 (2) 单词统计 (3) 单词查询及定位 (4) 退出
注意:
i) 文件至少包含50个英文单词(一定出现重复的单词,且一定包含数字)
ii) 文档不规范,单词之间可能不只有一个空格,并且有加引号的一些单词“jiaozi” 加引号的单词算单词,但数字不算单词
iii) 逐行扫描文本文件,计算文档中单词总数,及各个单词出现的频次,并且按照单词首字母abcd…… 的顺序排列,显示并生成soft.txt文件
iv) 查询任意给出的单词是否存在,如果不存在,在屏幕上输出“查无此词!”;如果存在,显示单词 出现的总次数,并且详细列出其 出现的位置。
例如:
请您输入待查询的词:of
单词 of 共出现了2次;
第1次出现在第1行,第5个位置;
第2次出现在第3行,第1个位置。
【具体代码】
public class total {
public static void main(String[] args) throws IOException {
Scanner scanf = new Scanner(System.in) ;
System.out.println("请输入用户的 ID 号");
String ID = scanf.next() ;
while(true) {
if(judge(ID)) {
System.out.println(""+ "(1) 建立文件\r\n" + "(2) 单词统计 \r\n" + "(3) 单词查询及定位\r\n"+"(4) 退出");
break;
}
else {
System.out.println("ID格式错误,请重新输入");
ID = scanf.next() ;
}
}
//不执行退出时始终为真
boolean rand = true ;
//选项
String choice;
List<String> list = new ArrayList<>();
while(rand) {
choice = scanf.next() ;
switch(choice) {
case "1": {
System.out.println("开始建立文件");
//每次读入一行
BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in));
String a;
while(true){
a = buffer.readLine();;
if("-1".equals(a)) {
break;
} else {
list.add(a);
}
}
createContentFile(list);
createSoftFile(list);
System.out.println("创建成功");
System.out.println(""+ "(1) 建立文件\r\n" + "(2) 单词统计 \r\n" + "(3) 单词查询及定位\r\n"+"(4) 退出");
break;
}
case "2":{
System.out.println("开始单词统计");
readFile();
System.out.println(""+ "(1) 建立文件\r\n" + "(2) 单词统计 \r\n" + "(3) 单词查询及定位\r\n"+"(4) 退出");
break;
}
case "3":{
System.out.println("开始单词查询及定位") ;
System.out.println("请您输入待查询的词:");
String check = scanf.next();
while (!"-1".equals(check)){
checkWord(list, check);
System.out.println("请您输入待查询的词: 输入-1结束");
check = scanf.next();
}
System.out.println(""+ "(1) 建立文件\r\n" + "(2) 单词统计 \r\n" + "(3) 单词查询及定位\r\n"+"(4) 退出");
break;
}
case "4":{
System.out.println("成功退出") ;
rand = false;
break;
}
default: System.out.println(""+ "(1) 建立文件\r\n" + "(2) 单词统计 \r\n" + "(3) 单词查询及定位\r\n"+"(4) 退出");
}
}
}
/**判断id格式是否正确*/
public static boolean judge(String id) {
String regex = "^[A-Z]{2}[0-9]{4}$";
boolean flag = id.matches(regex);
return flag;
}
/**创建ContentFile*/
public static void createContentFile(List<String> list) throws IOException {
String filePath = "G:\\文本文件单词统计1\\生成文本";
File dir = new File(filePath);
// 一、检查放置文件的文件夹路径是否存在,不存在则创建
if (!dir.exists()) {
dir.mkdirs();// mkdirs创建多级目录
}
File checkFile = new File(filePath + "/content.txt");
FileWriter writer = null;
try {
// 二、检查目标文件是否存在,不存在则创建
if (!checkFile.exists()) {
checkFile.createNewFile();// 创建目标文件
}
// 三、向目标文件中写入内容
// FileWriter(File file, boolean append),append为true时为追加模式,false或缺省则为覆盖模式
//先清空文件
writer = new FileWriter(checkFile);
writer.append("");
writer.flush();
//再追加写入
writer = new FileWriter(checkFile, true);
for (String content : list){
writer.append(content);
}
writer.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (null != writer) {
writer.close();
}
}
}
/**创建SoftFile*/
public static void createSoftFile(List<String> list) throws IOException {
//String为单词,countNum为单词出现的次数
Map<String, Integer> map = new HashMap<>();
int countNum = 0;
//正则表达式
Pattern p = Pattern.compile("\\b[a-zA-Z-]+\\b");
Matcher m = p.matcher(list.toString());
//find方法扫描输入序列以查找与该模式匹配的下一个子序列
while(m.find()){
//String group():返回匹配到的子字符串
String mstr = m.group();
//containsKey() 判断Map集合对象中是否包含指定的键名
if(map.containsKey(mstr)){
map.put(mstr, map.get(mstr)+1);
}else{
map.put(mstr, 1);
}
countNum++;
}
Set<String> set = map.keySet();
String[] strings = set.toArray(new String[set.size()]);
//忽略大小写排序
Arrays.sort(strings, String.CASE_INSENSITIVE_ORDER);
String filePath = "G:\\文本文件单词统计1\\生成文本";
File dir = new File(filePath);
// 一、检查放置文件的文件夹路径是否存在,不存在则创建
if (!dir.exists()) {
dir.mkdirs();// mkdirs创建多级目录
}
File checkFile = new File(filePath + "/soft.txt");
FileWriter writer = null;
try {
// 二、检查目标文件是否存在,不存在则创建
if (!checkFile.exists()) {
checkFile.createNewFile();// 创建目标文件
}
// 三、向目标文件中写入内容
// FileWriter(File file, boolean append),append为true时为追加模式,false或缺省则为覆盖模式
writer = new FileWriter(checkFile, false);
writer.append("共有单词:" + countNum + "个" + "\n");
for (String key : strings){
writer.append(key + ":" + map.get(key) + "次" + "\n");
}
writer.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (null != writer) {
writer.close();
}
}
}
/**读取文件*/
public static void readFile() {
FileReader fileReader = null;
BufferedReader br = null;
String line;
try {
// 判断文件是否存在
File testFile = new File("G:\\文本文件单词统计1\\生成文本\\soft.txt");
if(!testFile.exists()) {
System.out.println(testFile.getName() + "文件尚未生成");
}
// 读取文件
fileReader = new FileReader(testFile);
br = new BufferedReader(fileReader);
line = br.readLine();
while(line != null) {
System.out.println(line);
line = br.readLine();
}
}catch(Exception e) {
e.printStackTrace();
}
finally {
if(br != null) {
try {
br.close();
}catch(Exception e) {
e.printStackTrace();
br = null;
}
}
if(fileReader != null) {
try {
fileReader.close();
}catch(Exception e) {
e.printStackTrace();
}
}
}
}
/**查询单词*/
public static void checkWord(List<String> list, String check){
Map<Integer,Integer> map = new HashMap<>();
int count = 0;
for (int i = 0 ; i < list.size() ; i++){
int flag = list.get(i).indexOf(check);
if (flag != -1){
map.put(i+1, flag+1);
count++;
}
}
if (count == 0){
System.out.println("查无此词!");
}else {
System.out.println("单词" + check+"共出现了" + count + "次;");
int a = 1;
for(Map.Entry<Integer, Integer> entry : map.entrySet()){
System.out.println("第" + a + "次" + "出现在第" + entry.getKey() + "行," + "第" + entry.getValue() + "个位置");
a++;
}
}
}
}
【运行示例】
请输入用户的 ID 号
AD1234
(1) 建立文件
(2) 单词统计
(3) 单词查询及定位
(4) 退出
1
开始建立文件
what is the most important thing in the world?
I think it is health
You can take away our money,house,car,or even our clothes and we can survive.
That is why we always try to eat in a healthy way and exercise regularlg.
0
-1
创建成功
(1) 建立文件
(2) 单词统计
(3) 单词查询及定位
(4) 退出
2
开始单词统计
共有单词:45个
a:1次
always:1次
and:2次
away:1次
can:2次
car:1次
clothes:1次
eat:1次
even:1次
exercise:1次
health:1次
healthy:1次
house:1次
I:1次
important:1次
in:2次
is:3次
it:1次
money:1次
most:1次
or:1次
our:2次
regularlg:1次
survive:1次
take:1次
That:1次
the:2次
thing:1次
think:1次
to:1次
try:1次
way:1次
we:2次
what:1次
why:1次
world:1次
You:1次
(1) 建立文件
(2) 单词统计
(3) 单词查询及定位
(4) 退出
3
开始单词查询及定位
请您输入待查询的词:
we
单词we共出现了2次;
第1次出现在第3行,第63个位置
第2次出现在第4行,第13个位置
请您输入待查询的词: 输入-1结束
that
查无此词!
请您输入待查询的词: 输入-1结束
That
单词That共出现了1次;
第1次出现在第4行,第1个位置
请您输入待查询的词: 输入-1结束
-1
(1) 建立文件
(2) 单词统计
(3) 单词查询及定位
(4) 退出
4
成功退出
Process finished with exit code 0