最小错误记录,为什么行数相同的时候,输出不对
import java.util.*;
/*问题描述:
* 开发一个简单错误记录功能小模块,能够记录出错的代码所在的文件名称和行号。
处理:
1.记录最多8条错误记录,对相同的错误记录(即文件名称和行号完全匹配)只记录一条,错误计数增加;(文件所在的目录不同,文件名和行号相同也要合并)
2.超过16个字符的文件名称,只记录文件的最后有效16个字符;(如果文件名不同,而只是文件名的后16个字符和行号相同,也不要合并)
3.输入的文件可能带路径,记录文件名称不能带路径
输入描述:
一行或多行字符串。每行包括带路径文件名称,行号,以空格隔开。
文件路径为windows格式
如:E:\V1R2\product\fpgadrive.c 1325
*
* */
public class Main {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
Map<String,Integer> map=new
HashMap<String,Integer>();
while(s.hasNext()){
String FileDir=s.next();
String
FileName=FileDir.substring(FileDir.lastIndexOf("\\")+1);
int row=s.nextInt();
String key=FileName+" "+row;
if(map.containsKey(key)){
map.put(key, map.get(key)+1);
}else{
map.put(key, 1);
}
}
List<Map.Entry<String,Integer>> list=new
ArrayList<Map.Entry<String,Integer>>(map.entrySet());
//排序
Collections.sort(list,new
Comparator<Map.Entry<String, Integer>>(){
public int compare(Map.Entry<String,
Integer>o1,Map.Entry<String, Integer>o2){
return
o2.getValue().compareTo(o1.getValue());
}
});
//输出
for(int
i=0;i<list.size()&&i<8;i++){
Map.Entry<String,Integer>
entry=list.get(i);
String []
file=entry.getKey().split(" ");
String fileName=file[0];
String sub=
fileName.length()>16?fileName.substring(fileName.length()-16):fileName;
System.out.print(sub+"
"+file[1]+" "+entry.getValue()+" ");
}
}
}