文章目录

  • 问题描述
  • 思路
  • 代码

问题描述

已知有一个正确单词索引表(保存在当前目录下的文件index.txt中,且全为小写字母,按照字典序由小到大排列,每个单词独占一行),编写程序利用该单词表对某一英文文章(保存在当前目录下的另一个文件in.txt中)进行单词正确性检查,若该英文文章中出现的单词(只有连续字母组成)没有出现在单词索引文件中(检查时大小写无关),则将该出错的单词(其中的字母全部转换为小写)输出到当前目录下的另一文件error.txt中,每个单词独占一行,并且以字典序由小到大的顺序输出
假设:
1、in.txt中的文章有可能没有经过排版,格式有可能杂乱无章,也有可能没有写完整。
2、index.txt中的单词个数不超过1000个,每个单词的长度不超过50个字母
3、若出错的单词多次出现,则多次输出。

【输入形式】

保存单词索引表的文件index.txt和保存英文文章的文件in.txt都位于当前目录下。

【输出形式】

将出错的单词以字典序由小到大的顺序输出到当前目录下的文件error.txt中,每个单词单独占一行,多次出错的单词多次输出。若没有出现错误单词,则什么也不输出

【样例输入1】

假设文件in.txt内容为:

There are two verrsions of the international standards for C.
Thee first version was ratified in 1989 by the American National
Standards Institue (ANS1) C standard committee.It is often
referred as ANS1 C or C89. The secand C standard was completed
in 1999. This standard is comonly referred to as C99. C99 is a
milestone in C's evolution into a viable programing languga
for numerical and scientific computing.

文件index.txt中的单词索引表内容为:

a
american
and
ansi
are
as
by
c
committee
commonly
completed
computing
evolution
first
for
in
institue
international
into
is
it
language
milestone
national
numerical
of
often
or
programming
ratified
referred
s
scientific
secand
standard
standards
the
there
this
to
two
version
versions
viable
was

【样例输出1】

文件error.txt中出错的单词应为:

ans
ans
comonly
languga
programing
thee
verrsions

【样例1说明】

用index.txt中的单词索引表对in.txt中出现的每一个单词进行检查,检查时大小写无关,所以第一个单词There出现在索引表中,不是错误单词;单词verrsions没有出现在索引表中,拼写错误,所以作为出错单词输出;单词ANSI拼写成了ANS1,将其中字母都转换为小写后输出,并且多次出现,多次输出;其他出错单词类似。错误单词输出按照字典序由小到大输出到error.txt文件中。

【样例输入2】

假设文件in.txt内容为:

There are two versions of the international standard fo

文件index.txt中的单词索引表内容为:

are
for
international
of
standards
the
there
two
versions

【样例输出2】

文件error.txt中出错的单词应为:

fo
standard

【样例2说明】

文件in.txt中的单词standard没有出现在索引表文件index.txt中,所以作为错误单词输出。

注意:样例2中in.txt文件内容还不完整,最后的单词fo后没有任何字符,fo也没有出现在索引表中,所以也作为错误单词输出。

思路

1、从两个文件中读取单词,并且存入ArrayList中

2、通过listIndex,判断listIn中是否有错误单词,及未出现的单词(循环查找错误单词),找到之后在进行排序

3、最后将错误的单词存入error.txt中

代码

import java.io.*;
import java.util.ArrayList;
import java.util.Comparator;public class Match {public static void main(String [] args){File fileIn = new File("in.txt");File fileIndex = new File("index.txt");ArrayList<String> listIn = new ArrayList<String>();ArrayList<String> listIndex = new ArrayList<String>();ArrayList<String> listError=new ArrayList<String>();listIn = Input(fileIn);listIndex = Input(fileIndex);
//        System.out.println(fileIndex);listError=matchError(listIn,listIndex);
//        show(listError);writerError(listError);}//从文件中读取单词public static ArrayList Input(File file){BufferedReader br = null;ArrayList<String> listIn = new ArrayList<String>();try {br = new BufferedReader(new FileReader(file));String str = null;while((str = br.readLine()) != null) {String[] wordsArr1 = str.split("[^a-zA-Z]");  //过滤出只含有字母的for (String word : wordsArr1) {if(word.length() != 0){  //去除长度为0的行listIn.add(word.toLowerCase());   //toLowCase()统一转成小写
//                        System.out.println(word);}}}br.close();} catch (IOException e) {e.printStackTrace();}return listIn;}//查找错误单词并排序public static ArrayList matchError(ArrayList listIn,ArrayList listIndex){ArrayList<String> listError=new ArrayList<String>();for(int i=0;i<listIn.size();i++){if(!listIndex.contains(listIn.get(i))){     //contains() 方法用于判断元素是否在动态数组中listError.add((String) listIn.get(i));}}listError.sort(Comparator.naturalOrder());    //naturalOrder() 方法指定元素以自然顺序(升序)排序。return listError;}public static void show(ArrayList list){System.out.println("size:"+list.size());for(int i=0;i<list.size();i++){System.out.println(list.get(i));}}//将错误单词写入error.txt中public static void writerError(ArrayList listError){File file = new File("error.txt");
//        if(!file.exists()) {//           try{//               file.createNewFile();
//           }catch (IOException e){//               e.printStackTrace();
//           }
//        }try {BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file)));for(int i=0;i<listError.size();i++){out.write((String) listError.get(i)+'\n');}out.flush();out.close();} catch (IOException e) {e.printStackTrace();}}
}

单词检查:判断文件中的英文单词是否正确,若不正确,排序输出到另一个文件中相关推荐

  1. 从文件中读取数据,排序之后输出到另一个文件中

    文件中有一组数据,要求排序后输出到另一个文件中去 主要有两个知识点: 排序.文件操作 C++/C代码如下: [cpp] view plaincopy #include<iostream> ...

  2. 文件中有一组整数,要求排序后输出到另一个文件中

    这个主要复习一下文件输入输出流~~ 1 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 ...

  3. 将一个数组中的字符串用指定字符分割开,分别放到另一个数组中

    #include "stdafx.h" #include <stdio.h> #include <string.h> #pragma warning(dis ...

  4. C语言100题第二题 编写函数fun()的功能并调用:从3个红球,5个白球,6个黑球中任意取8个 作为一组,进行输出。在每组中,可以没有黑球,但是必须有红球和白球。组合数作为函数返回值。

    结构:分析-代码-总结 原题 分析 代码 总结 原题 编写函数fun()的功能并调用:从3个红球,5个白球,6个黑球中任意取8个 作为一组,进行输出.在每组中,可以没有黑球,但是必须有红球和白球.组合 ...

  5. php获取目录文件 排序输出,php实现对文件夹目录中的文件进行排序的方法

    php实现对文件夹目录中的文件按照时间.名称.大小进行排序的方法 函数介绍: opendir() 函数打开目录句柄. readdir() 函数返回目录中下一个文件的文件名. array_multiso ...

  6. java 打印到文件_Java I / O:如何将循环输出打印到一个文件...

    我正在尝试将多个联系人写入并保存到一个文件中,然后将其存储为十六进制文件,但它会不断创建多个文件(每个联系人一个).我试图移动: System.out.print("Type a name ...

  7. python中如何编写代码输入多个数据并把它们放在一个列表中去_10分钟学习函数式Python...

    在这篇10分钟的文章中,您将学习Python中的函数式范型.您还将学习列表推导式. 目录 函数式范式 Python的map函数是如何运行的 Python中的lambda表达式 Python中的redu ...

  8. python中如何编写代码输入多个数据并把它们放在一个列表中去_编写高质量Python代码的59个有效方法,你用过几个...

    欢迎点击右上角关注小编,除了分享技术文章之外还有很多福利,私信学习资料可以领取包括不限于Python实战演练.PDF电子文档.面试集锦.学习资料等. 这个周末断断续续的阅读完了<Effectiv ...

  9. python中如何编写代码输入多个数据并把它们放在一个列表中去_这59条编写高质量Python代码的方法你知道吗?...

    这个周末断断续续的阅读完了<Effective Python之编写高质量Python代码的59个有效方法>,感觉还不错,具有很大的指导价值. 下面将以最简单的方式记录这59条建议,并在大部 ...

最新文章

  1. 干货丨从线性回归到无监督学习,数据科学家需要掌握的十大统计技术
  2. 【Flutter】Flutter 拍照示例 ( 拍照并获取照片源码示例 | image_picker: ^0.5.2 版本 )
  3. SpringSecurity认证流程分析
  4. tensorflow python3.6_[教程]Tensorflow + win10 + CPU + Python3.6+ 安装教程
  5. android重新编译res,使用 gradle 在编译时动态设置 Android resValue / BuildConfig / Manifes中lt;meta-datagt;变量的值...
  6. python3语音控制电脑_python语音控制电脑_uusee全屏
  7. 常用的比较排序算法总结
  8. Android中ICS4.0源码Launcher启动流程分析【android源码Launcher系列一】
  9. 100个javaweb实战项目(视频+源码+文档),带你上天!
  10. 《Java程序设计》期末复习资料
  11. fileupload控件的属性_FileUpload控件的配置 .
  12. 解决局域网共享无法访问
  13. 风变编程python24_如何看待风变编程的 Python 网课?
  14. 防抖与节流的原理、实现及优化
  15. ISE在win10中闪退解决方法以及ISE14.7安装包
  16. 硅谷的政治泡沫:反对特朗普,与美国大部分地区观念出现割裂
  17. 深入了解 Flex 属性
  18. 在IDEA中给项目同时配置git和svn
  19. java jdk8 使用stream实现两个list集合合并成一个list集合(对象属性的合并)
  20. 新冠「阳了」,如何稳定情绪,做好心理自救?

热门文章

  1. 计算机电缆与光缆有什么区别,计算机电缆和普通电缆区别
  2. 理解‘*‘,‘*args‘,‘**‘,‘**kwargs‘
  3. Linux 新建文件命令
  4. mp3解码 - 文档参考
  5. Js--字符串拼接/连接
  6. VIRTIO VHOST
  7. SQL server数据库的备份与还原遇到的问题
  8. Matlab中用fft作频谱后为什么要用fftshift
  9. 再说曾头市史文恭只 水浒
  10. Excel常用技巧--工作中最常用的30个Excel函数公式