题解 | #简单错误记录#

简单错误记录

http://www.nowcoder.com/practice/2baa6aba39214d6ea91a2e03dff3fbeb

import java.util.Scanner;

public class Main { //由于仍以第一次出现的时间为准的循环记录,拟采用窗口的方式实现。 public static void main(String[] args) { //输入 Scanner scanner=new Scanner(System.in); MyError myError[]=new MyError[100];//储存错误记录 for (int i = 0; i < 100; i++) { myError[i]=new MyError(); } int i=0;//窗口结束位置

    //填入第一个记录
    String line=scanner.nextLine();
    String tmp1[]= line.split(" ")[0].split("\\\\");
    int intTmp= Integer.parseInt(line.split(" ")[1]);
    //裁剪文件名
    if (tmp1[tmp1.length-1].length()>16)
        tmp1[tmp1.length-1]=tmp1[tmp1.length-1].substring(tmp1[tmp1.length-1].length()-16,
                tmp1[tmp1.length-1].length());
    myError[i].setName(tmp1[tmp1.length-1]);
    myError[i].setLineCount(intTmp);
    myError[i].setCount(1);


    outter:while(scanner.hasNextLine()){
        line=scanner.nextLine();
        if (line.equals(""))break;
        String s=line.split(" ")[0];
        String tmp[]= s.split("\\\\");
        intTmp= Integer.parseInt(line.split(" ")[1]);
        //裁剪文件名
        if (tmp[tmp.length-1].length()>16)tmp[tmp.length-1]=tmp[tmp.length-1].substring(tmp[tmp.length-1].length()-16,tmp[tmp.length-1].length());
        for (int j = 0; j <= i; j++) {
            if (myError[j].getName().equals(tmp[tmp.length-1])
                    &&myError[j].getLineCount()==intTmp) {
                myError[j].setCount(myError[j].getCount()+1);
                continue outter;
            }
        }
        i++;
        myError[i].setName(tmp[tmp.length-1]);
        myError[i].setLineCount(intTmp);
        myError[i].setCount(1);
    }

    //输出窗口
    if (i>=8){
        for (int j = i-7; j <=i ; j++) {
            System.out.println(myError[j].getName()+" "+myError[j].getLineCount()+" "+myError[j].getCount());
        }
    }
    else{
        for (int j = 0; j <=i; j++) {
            System.out.println(myError[j].getName()+" "+myError[j].getLineCount()+" "+myError[j].getCount());
        }
    }
}

private static class MyError {
    String name="";
    int lineCount=0;
    int count=0;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getLineCount() {
        return lineCount;
    }

    public void setLineCount(int lineCount) {
        this.lineCount = lineCount;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }
}

}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务