栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

单词统计题

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

单词统计题

做一个简单的JAVA程序!
题目: 文本文件单词统计
问题描述:编写一个文本文件单词统计的程序,包括建立文件、单词统计、单词查询、单词定位的功能。
基本要求:
程序应先询问用户的 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 static void main(String[] args)
	 {   
		 ArrayList gloList=new ArrayList();
		 String gloFilepath=null;
		 int glonum=0;
		 int fileNum=0;
		 System.out.println("请输入用户名ID:");
		 String	str=sc.nextLine();
		 //方法判断用户名
		 str=WordOperation.identify(str);
	      System.out.println("***********************************");
//文件 
            try {
				File dir=new File("D:\WordCount");//创建目录
				 if(!dir.exists())
				 {
					 dir.mkdir();
				 }
				 //创建用户目录
				 File userdir=new File("D:\WordCount\"+str);
				 if(!userdir.exists())
				 {
					 userdir.mkdirs();
				 }
				 File userdir_soft=new File("D:\WordCount\"+str+"_soft");
				 if(!userdir_soft.exists())
				 {
					 userdir_soft.mkdirs();
				 }
				 fileNum=WordCountFile.countFileNumber(userdir);
				boolean flag = true;
				System.out.println("------------------------------");
				
				
				while(flag){ 
					System.out.println("请选择:n(1)建立文件n(2)单词统计n(3)单词查询及定位n(4)退出");
					int choice=WordOperation.rightInput();
					if(choice<0||choice>4)
					{
						System.out.println("所要求服务不在范围内,请重新输入!!!");
					}
					switch(choice) {
						case 1:{   
							int n=WordCountFile.countFileNumber(userdir);//当前用户所拥有的文件数
							File file = new File(userdir,str+"_"+(n+1)); 
							System.out.println("这是您的第"+(n+1)+"个文件");
							System.out.println("请输入您文件当中的内容:");
							fileNum=WordCountFile.create(file,str,n);   //创建后文件的个数
							System.out.println("文件个数为:"+fileNum);
							break;
						}
						case 2:{
							fileNum=WordCountFile.countFileNumber(userdir);//当前用户所拥有的文件数
							int m=WordOperation.rightInput(fileNum);//让用户正确输入
							glonum=m;
							String filepath="D:\WordCount\"+str+"\"+str+"_"+m;
							gloFilepath=filepath;
							ArrayList list=new ArrayList();
							list=WordCountFile.read(filepath);
							gloList=list;
							int num=WordOperation.count(list);			                
							System.out.println("文件中的单词个数为:"+num+"个");
							break;
						}
						case 3:{
						     System.out.println("还是要打开原文件吗?<1> 是 <2>否");
						     int ch=sc.nextInt();
						     String filepath;
						     int x;
						     if(ch!=1) {
						    	  x=WordOperation.rightInput(fileNum);//正确输入
						    	  glonum=x;
						    	 filepath="D:\WordCount\"+str+"\"+str+"_"+x;
						    	 gloFilepath=filepath;
						    	 gloList=WordCountFile.read(filepath);			    	
						     }
						     //查询单词
						     if(gloList.size()==0) //避免什么也没做就直接3 是
						     {
						    	 System.out.println("没有选择文件,请选择文件序号:");
						    	 x=sc.nextInt();
						    	 filepath="D:\WordCount\"+str+"\"+str+"_"+x;
						    	 gloList=WordCountFile.read(filepath);				    	
						     }
						     WordOperation.print(gloList);  //查询位置
							System.out.println("是否要统计单词频率?<1> 是 <2> 否");
							int w=sc.nextInt();
							if(w==1)
							{
								System.out.println("各个单词的频率为:");
								String str1=WordOperation.frequent(gloList);
								 String file=gloFilepath+"_"+"soft.txt";
								 String file_soft="D:\WordCount\"+str+"_soft"+"\"+str+"_"+gloNum+"_"+"soft.txt";
								WordCountFile.createJust(file_soft,str1);	
							}
							break;
						}
						case 4:{
							System.out.println("退出成功!");
							flag = false;
							break;
						}
					}
				}
			}
			catch(Exception e) {
				e.printStackTrace();
			}
		    sc.close();
		}

在主程序中,先对用户名进行验证,看其是否符合要求。
用户登录后,若其未曾登陆过,
进入程序后,询问用户需求,(1)建立文件n(2)单词统计n(3)单词查询及定位n(4)退出。输入对应数字,即进入对应服务。

先写到这里,有空了再补

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/288298.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号