2016 Huawei machine Questions--Simple records

Source: Internet
Author: User

The file name and line number where the code for the error is logged.
Processing:
1. Record up to 8 error records, the same error record (that is, the file name and line number exactly match) only one record, the error count increases; (the file is in a different directory, and the same file name and line number are merged)
2. More than 16 characters of the file name, only the last valid 16 characters of the file, (if the file name is different, but only the file name of the following 16 characters and the same line number, do not merge)
3. Input file may have path, record file name cannot take path

Input Description:
A line or multiline string. Each line includes a path file name, line number, separated by a space.
File path to Windows format
Example: E:\V1R2\product\fpgadrive.c 1325
Output Description:

Results are sorted according to the first occurrence of the input, depending on the number of orders, in the same number of times.
If more than 8 records are exceeded, only the first 8 records are printed.
If the file name is longer than 16 characters, only the first 16 characters are printed

Input Example:
E:\V1R2\PRODUCT\FPGADRIVE.C 1325
Output Example:
FPGADRIVE.C 1325 1

Code:
Importjava.util.ArrayList;Importjava.util.Collections;ImportJava.util.Comparator;ImportJava.util.HashMap;ImportJava.util.Scanner; Public classMain { Public Static voidMain (string[] args) {//Create an accepted keyboard entry objectScanner cin =NewScanner (system.in); //accept the file name or file pathString file =NULL; //gets the file path the last "\" Subscript index        intLastIndex = 0; //Get file name//accept the line number where the error occurred        intErrorLine = 0; //Create a hashmap<errlog>hashmap<string, errlog> HashMap =NewHashmap<string, errlog>(); ErrLog ErrLog=NULL; String Key=NULL; StringBuilder Sbbuilder=NewStringBuilder ();  while(Cin.hasnext ()) {//The first parameter is a file name or a file pathFile =Cin.next (); //Get error line numberErrorLine =Cin.nextint (); //gets the index of the last ' \ 'LastIndex = file.lastindexof (' \ \ '); //If no description is found and the path is not, return the file name directly//is the path to intercept \ followed by the string as the file namesbbuilder. Append (LastIndex< 0? File:file.substring (LastIndex + 1). Append (" "). Append (ErrorLine); Key=sbbuilder.tostring (); Sbbuilder.setlength (0); //adding error records into HashMapErrLog =Hashmap.get (key); if(ErrLog = =NULL) {                //if not present, new one and add it inHashmap.put (Key,NewErrLog (Key, 1)); } Elseerrlog.size++;//Is present, the number of errors is accumulated} cin.close (); //----------above is the statistical error message, the following is the value------------------arraylist<errlog> list =NewArraylist<errlog>(Hashmap.values ()); //depending on the number of errors, put the number of errors in front, if the number of errors consistent, will appear in the order of the earlier to sortComparator<errlog> CMP =NewComparator<errlog>() {@Override Public intCompare (ErrLog O1, ErrLog O2) {returnO2.size-o1.size = = 0? (O1.queuemark-o2.queuemark): O2.size-o1.size;        }        };        Collections.sort (list, CMP); //Remove the first 8, and output        intLENS = 8 > list.size ()? List.size (): 8;  for(inti = 0; I < lens; i++) {            //file name if larger than 16 bits also need to intercept file name 16 bit + line number outputErrLog =List.get (i); //This gets the index of the delimiter ', and then pushes forward 16 bits is the file name we want to output .LastIndex = ErrLog.name.lastIndexOf (""); LastIndex= lastIndex-16 < 0? 0:lastindex-16; //gets the file name and outputsSystem.out.println ((LastIndex = = 0?)errLog.name:errLog.name. Substring (lastIndex))+ " " +errlog.size); }    }} classErrLog {//This class is not standard and is only convenient for this topic.    Static intMark//Auxiliary Implementation field Queuemark increment in order of occurrenceString name;//filename Stores the wrong file name, consisting of the full file name, and the error line number.     intQueuemark;//the order in which the markers appear    intSize//Count the number of errors that occurred      PublicErrLog (String name,intsize) {        Super();  This. Name =name;  This. Queuemark = mark++;  This. Size =size; }}

2016 Huawei machine Questions--Simple records

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.