0蓝海水0 2014-04-21 13:45 采纳率: 0%
浏览 2423

一个关于文本匹配的问题,程序设计

建立一个文本文件,其内容如下。对于任一给定的单词,在该文本中找寻匹配的字符串,输出结果

Information security means protecting
information and information systems from unauthorized access, use, disclosure,
disruption, modification, or destruction. The terms information security,
computer security and information assurance are frequently used
interchangeably.

具体要求:

1、实现文件的读取操作,而不是将文本以字符串形式预存于程序中

2、进行单词匹配时,不区分大小写

3、统计需要匹配的单词在该文本中出现的次数和位置(即该单词是文本的第几个单词),并输出
4、对于基本匹配但不完全匹配的单词,能够给出提醒信息(例如,当用户试图匹配单词“securite”时,系统能够提示用户,是否是需要匹配“security”)
最好是java的图形化界面,其他的也可以

  • 写回答

1条回答

  • 晴天_SH 2014-04-22 11:41
    关注

    对于这个题我的思路是:先从文件中读入内容到程序,然后对于用户输入的目标串进行匹配,匹配成功的结果存入Map中,文本扫描完毕,Map中的键值对就是所要求的目标串的位置和值,假若匹配不成功,则才从头检索目标串的子串,循环进行查找,找到相近的就是你所说的基本匹配 我写的代码如下,不足之处欢迎指正:

    public class FileTest
    {
    public static void main(String[] args)throws Exception
    {
    int count=0,index=0;
    HashMap map=new HashMap();
    Scanner sc=null;
    boolean flag=false;

        System.out.println("待查找的目标串为:");
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        String mode = br.readLine();
    
        sc=new Scanner(new File("F:\\myclass\\test.txt"));
        while(sc.hasNext())
        {
            index++;
            if(sc.next().equalsIgnoreCase(mode))
            {
                flag=true;
                map.put(""+index,sc.next());
                count++;
            }
        }
        if(flag)
        {
            System.out.println("查到目标串count:"+count+"的分别为:");
            for(Object key:map.keySet())
                System.out.println("索引Key:"+key+"----->"+map.get(key));
        }
        else
        {
            sc=new Scanner(new File("F:\\myclass\\test.txt"));
            q:while(mode.length()>0)
            {
                mode=mode.substring(0,mode.length()-1);
                while(sc.hasNext())
                {
                    String s=sc.next();
                    if(s.compareTo(mode)==(s.length()-mode.length()))
                    {
                         System.out.println("未找到目标串,但是找到相似的串"+s);
                         break q;
                    }
                }
            }
    
        }
    }
    

    }

    评论

报告相同问题?

悬赏问题

  • ¥15 MCNP里如何定义多个源?
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏