Word Count
github Address: https://github.com/imenist/WordCount
A. Project requirements
1.wc.exe is a common tool, it can count the number of characters of text files, words, and lines. The project asked to write a command line program, to mimic the function of the existing wc.exe, and be expanded, given the number of characters in a programming language source files, words, and lines
2. Implement a statistical program, the number of characters it can correct statistics program file, the number of words, lines, and also has other extended features, and the ability to quickly process multiple files. Program processing mode as user requirements: wc.exe [parameter] [file_name]
3. Basic requirements:
wc.exe -c file.c // returns the number of characters in the file file.c
The number of wc.exe -w file.c // Returns the file of the word file.c
wc.exe -l file.c // returns the number of rows of file file.c
4. Extensions:
Under -s Process directories recursively eligible files
-a return more complex data (line / space lines / comment lines).
5. Advanced features:
-x parameter. This parameter is used alone. If you have the command line parameters, the program will display a graphical interface, the user can select a single file through the interface, the program will display the file number of characters, lines, etc. All statistics.
Two .PSP table
PSP2.1 | Personal Software Process Stages | Estimated time (min) | The actual time-consuming (minutes) |
Planning | plan | 10 | 10 |
-Estimate | - How much time is estimated that the task requires | 265 | 400 |
Development | Develop | 250 | 400 |
-Analysis | - needs analysis (including learning new technologies) | 20 | 20 |
-Design Spec |
- Generate design documents | 30 | 20 |
-Design Review | - Design review (and colleagues reviewed the design documents) | 20 | 10 |
-Coding Standard | - code specification (currently the developers of the development of appropriate norms too) | 10 | 10 |
-Design | - specific design | 30 | 80 |
-Coding | - specific coding | 90 | 200 |
-Code Review | - Code Review | 30 | 10 |
-Test | - test (self-test, modify the code, submit modifications) | 20 | 50 |
Reporting | report | 50 | 60 |
-Test Report | -testing report | 30 | 40 |
-Size Measurement | - computational effort | 10 | 10 |
-Postmortem & Process Improvement Plan | - hindsight, and propose process improvement plan | 10 | 10 |
total | 310 | 470 |
III. Problem-solving ideas
1. Basic requirements:
- Statistics Number of characters: read the file line by line, and then count the number of characters in each line, and then added together.
- Count the number of words: read the file line by line, and then by determining the statistical relationship between the number of words with a space character, and then added together.
- Statistics line: read the file line by line, direct accumulate
2. Advanced Features
- Recursive process to meet the requirements of the document: to determine whether a file or folder, if the file is returned, the folder continues recursively until you find the file, and then by the regular expression screened to meet the requirements of the file (the start is to use the file name suffixes name to a simple distinction, but this can not be fuzzy search, ie sign, then learn to use regular expressions to complete the requirements of the more successful)
- Statistical complex data (line / space lines / comment lines): read the file line by line, setting a Boolean variable if the condition is satisfied accumulation
- Graphical interface: to call the java Swing set up, manage the layout of the control (start direct use of containers to add, is only found when running the last added will be displayed, and later found a lot of documents, and view the source code jframe to find the problem, there is no set layout)
IV. Design implementation
1. The main categories:
- The main function of the project: main
- wordCount: detailed data such as the number of characters, words, etc., return as an object
- readFile: read the file
- Recursive folders and files: travelFile
- count: wordCount bottom, wherein there are a plurality of methods for data acquisition
- frameUI: be gui interface design
2. Run the program flow chart:
V. Code Description
1. Return the number of characters
public int getCharNum(String str){ char[] cn = str.toCharArray(); return cn.length; }
2. Return the number of words
public int getWordNum(String str){ boolean flag = false; int num = 0; int length; char[] wn = str.toCharArray(); length = wn.length; for(int i=0;i<length;i++){ //当是一个新单词的时候才+1 if(((wn[i]>=65 && wn[i]<=90) || (wn[i]>=97 && wn[i]<=122)) && flag == false){ flag = true; num++; }the else IF ((Wn of [I]> = 65 && Wn of [I] <= 90) || (Wn of [I]> = 97 && Wn of [I] <= 122 )) { // If not, new words skipped Continue ; } the else { // on the end of a word is set to false In Flag = to false ; } } return NUM; }
3. Return the number of rows comment
public int getNoteLineNum (the ArrayList <String> STR) { int NUM = 0 ; Boolean In Flag = to false ; for ( int I = 0; I <str.size (); I ++ ) { String TEMP = str.get (I) .trim (); // single line comment directly +1 IF (temp.startsWith ( "//" )) NUM + = 1 ; // multi-line comments, and + 1's first set of Boolean variables, known encountered * / + 1 until end the else IF (temp.startsWith ( "/ *" )) { NUM + =. 1 ; In Flag= true; } else if(temp.endsWith("*/")){ num += 1; flag = false; } else if(flag == true) num += 1; } return num; }
4. recursive processing file
The ArrayList <String> = pathsFile new new the ArrayList <String> (); File File = new new File (FILESPATH); // build object ft recursive processing fileTravel ft = new new fileTravel (pathsFile, SY); ft.travel (File); int = length pathsFile.size (); // the file obtained wordCount process and output for ( int I = 0; I <length; I ++ ) { String the currentFile = pathsFile.get (I); wc.count (the currentFile); System.out.println ("File:" + the currentFile); Switch (choose1) { Case "-C": System.out.println ( "File number of characters:" + wc.getCharNum ()); BREAK ; Case "-w": the System.out .println ( "The number of files the word:" + wc.getWordNum ()); BREAK ; Case "the -l": System.out.println ( "file line number:" + wc.getLineNum ()); BREAK ; Case "- A " : System.out.println ( " file number of characters: "+ wc.getCharNum ()); the System.out.println ( "The number of files the word:" + wc.getWordNum ()); System.out.println ( "file line number:" + wc.getLineNum ()); System.out.println ( "File lines of code:" + wc.getCodeLine ()); System.out.println ( "File Notes line number: "+ wc.getNoteLine ()); System.out.println ( " the number of blank lines file: "+ wc.getEmptyLine ()); BREAK ; } } BREAK ; public class fileTravel { public the ArrayList <String> filepaths; Matcher m; String his =null ; public fileTravel (the ArrayList <String> filepaths, String SY) { the this .filePaths = filepaths; the this .SY = SY; } public fileTravel (the ArrayList <String> filepaths) { the this .filePaths = filepaths; } public void Travel (File file) { // regular expressions, wildcards to find and filter the files meet the requirements of (here txt file) String sy1 = "^ [" + SY + "] * (txt) $.." ; Pattern the p- = Pattern.compile (sy1); IF (File == null ) return ; m = p.matcher (file.getName ()); // if the file only to find matching string array which will be called in the file into IF (m.find () || SY == null ) filePaths.add (file.getAbsolutePath ()); iF (file.isDirectory ()) { for (files file: File.listFiles ()) { // if the folder is continued recursively Travel (files); } } } }
5.gui Design
public frameUI () { // get the object Jframe JFrame = new new the JFrame (); // set the title jFrame.setTitle ( "Word the Count" ); // set the window size jFrame.setBounds (600,600,800,100 ); jFrame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE ); // get editing window object jFrame.getContentPane () the Add (the getContentPane (.)); the this .setLayout ( new new the FlowLayout ()); // initializes each control parameter window the JLabel label = new new the JLabel (); label.setText ( "file name:" ); JButton button = new JButton("选择文件"); JButton button1 = new JButton("选择文件夹"); JButton _c = new JButton("-c"); JButton _w = new JButton("-w"); JButton _l = new JButton("-l"); JButton _a = new JButton("-a"); JButton _s = new JButton("-s"); textField = new JTextField(20); // Add the control to the window getContentPane () the Add (label);. . GetContentPane () the Add (textField); getContentPane () the Add (the Button);. GetContentPane () the Add (Button1);. GetContentPane () the Add. (_c); . getContentPane () the Add (_w); getContentPane () the Add (_l);. . getContentPane () the Add (_a); . getContentPane () the Add (_s); // when selecting a file, call comes fileChooser and obtains the file object, its path to return to the text box button.addActionListener ( new new the ActionListener () { @Override public void the actionPerformed (the ActionEvent E) { int I =fileChooser.showOpenDialog (the getContentPane ()); IF (I == JFileChooser.APPROVE_OPTION) { selectedFile = fileChooser.getSelectedFile () getAbsoluteFile ();. textField.setText (selectedFile.getName ()); } } }); // Select _s recursive functions when the folder, and will automatically meet the requirements of the file output in the form of dialog boxes which data _s.addActionListener ( new new the ActionListener () { @Override public void the actionPerformed (the ActionEvent E) { String filepaths = selectedFile.getAbsolutePath (); file the currentFile = new File(filePaths); try { fileTravelUI(currentFile); } catch (Exception e1) { e1.printStackTrace(); } } });
VI. Test Run
1. Basic functions
- Empty file:
- Only one character of the file:
- Only a word file
- One-line file
- A typical source file
2. extensions (-a, -s)
3. Advanced Features (gui operation)
- Select individual files to perform basic operations
- Select the folders recursively
VII. Project Summary
The first to do personal projects, many places are not very skilled, just understanding the question on the use of a lot of time. In the process I also learned a lot of previously unknown knowledge
- In the document screening, java's IO stream file with the screening of regular expressions can handle the vast majority of
- In gui design, the advantages and disadvantages between different layouts also learn a lot
- Before knocking the code, there is a PSP schedule allows me to use their time better
- Through their own people to do a project but also to understand the important work to do than just knock project codes, as well as design, ideas, etc.
By this time the personal project, I also understand their own lack of self-enrichment will continue to learn