Do a simple JAVA program!
Title: text file word statistics
Problem Description: write a text file word statistics program, including the functions of establishing files, word statistics, word query and word positioning.
Basic requirements:
The program should first ask the user's ID number (the ID number includes two uppercase letters and four digits), for example:
Please enter the user ID number: AB1234
The program shall verify the input ID number to meet the format required by the ID number, and then the program prompts four options:
(1) Create file
(2) Word statistics
(3) Word query and location
(4) Exit
be careful:
i) The file contains at least 50 English words (repeated words must appear and numbers must be included)
ii) the document is not standardized, there may be more than one space between words, and there are some quoted words "jiaozi". Quoted words are counted as words, but numbers are not counted as words
iii) scan the text file line by line, calculate the total number of words in the document and the frequency of each word, and according to the initial letter abcd
Display and generate the soft.txt file
iv) query whether any given word exists. If it does not exist, output "no such word!" on the screen; If present, displays the word
The total number of occurrences and the location of their occurrence are listed in detail.
For example, please enter the word to be queried: of
The word of appears twice in total;
The first time appears in line 1, position 5;
The second time appears in line 3, position 1.
Please enter the words to be queried:
Post the code first and explain later
Main program (control process):
public static void main(String[] args) { ArrayList<String> gloList=new ArrayList<String>(); String gloFilepath=null; int gloNum=0; int fileNum=0; System.out.println("enter one user name ID: "); String str=sc.nextLine(); //Method to determine user name str=WordOperation.identify(str); System.out.println("***********************************"); //file try { File dir=new File("D:\\WordCount");//Create directory if(!dir.exists()) { dir.mkdir(); } //Create user directory 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("Please select:\n(1)create file\n(2)Word statistics\n(3)Word query and location\n(4)sign out"); int choice=WordOperation.rightInput(); if(choice<0||choice>4) { System.out.println("The requested service is out of range, please re-enter!!!"); } switch(choice) { case 1:{ int n=WordCountFile.countFileNumber(userdir);//Number of files owned by the current user File file = new File(userdir,str+"_"+(n+1)); System.out.println("This is your second"+(n+1)+"Files"); System.out.println("Please enter the contents of your file:"); fileNum=WordCountFile.create(file,str,n); //Number of files created System.out.println("Number of files:"+fileNum); break; } case 2:{ fileNum=WordCountFile.countFileNumber(userdir);//Number of files owned by the current user int m=WordOperation.rightInput(fileNum);//Let the user input correctly gloNum=m; String filepath="D:\\WordCount\\"+str+"\\"+str+"_"+m; gloFilepath=filepath; ArrayList<String> list=new ArrayList(); list=WordCountFile.read(filepath); gloList=list; int num=WordOperation.count(list); System.out.println("The number of words in the file is:"+num+"individual"); break; } case 3:{ System.out.println("Do you still want to open the original file?<1> yes <2>no"); int ch=sc.nextInt(); String filepath; int x; if(ch!=1) { x=WordOperation.rightInput(fileNum);//Correct input gloNum=x; filepath="D:\\WordCount\\"+str+"\\"+str+"_"+x; gloFilepath=filepath; gloList=WordCountFile.read(filepath); } //Query word if(gloList.size()==0) //Avoid doing nothing directly 3 yes { System.out.println("No file selected, please select file serial number:"); x=sc.nextInt(); filepath="D:\\WordCount\\"+str+"\\"+str+"_"+x; gloList=WordCountFile.read(filepath); } WordOperation.print(gloList); //Query location System.out.println("Do you want to count word frequency?<1> yes <2> no"); int w=sc.nextInt(); if(w==1) { System.out.println("The frequency of each word is:"); 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("Exit succeeded!"); flag = false; break; } } } } catch(Exception e) { e.printStackTrace(); } sc.close(); }
In the main program, first verify the user name to see whether it meets the requirements.
After the user logs in, if he has not logged in,
After entering the program, ask the user's requirements, (1) create a file \ n(2) word statistics \ n(3) word query and positioning \ n(4) exit. Enter the corresponding number to enter the corresponding service.
Write here first and fill in when you are free