3

我正在尝试解析一个字符串并将其存储在一个字符串数组中。当我试图解析字符串“log1”时,我能够解析它。但是当我解析字符串“log2”时,得到这个“线程中的异常”main“java.lang.ArrayIndexOutOfBoundsException:1”。

运行以下代码:

import static java.lang.System.*;

public class test{


    static String[] final_log = new String[13];
    static String audit = null;
    static String[] auditArray = null;
    static int j=0;

    public static void main(String[] args) {



    String[] columnlist = {"UserID","ClientAddress","Severity","EventType","ResourceAccessed","EventStatus","CompulsoryEvent","AuditCategory","ComponentID","AuditDetails","App ID","Cluster ID","Node ID"};

    String log1 = "UserID : ccmadministrator  ClientAddress : 172.30.235.29  Severity : 5  EventType : GeneralConfigurationUpdate  ResourceAccessed: CUCMAdmin  EventStatus : Success  CompulsoryEvent : No  AuditCategory : AdministrativeEvent  ComponentID : Cisco CUCM Administration  AuditDetails :  record in table batjob with key field name = Export Configuration, Job id : 1380812040 added  App ID: Cisco Tomcat Cluster ID:  Node ID: iptapps-eft-cucm1" ;


    String log2 = "09:03:36.776 |LogMessage   UserID : ccmadministrator  ClientAddress : 172.30.238.14  Severity : 6  EventType : GeneralConfigurationUpdate  ResourceAccessed: Cisco CCM Serviceability RTMT  EventStatus : Success  CompulsoryEvent : No  AuditCategory : AdministrativeEvent  ComponentID : Cisco CCM Serviceability RTMT  AuditDetails : Alert status changed to Enable for the alert: Cisco Syslog Agent:SYSAGENT:SyslogSeverityMatchFound App ID: Cisco Tomcat Cluster ID:  Node ID: iptapps-eft-cucm1";

    auditArray = log2.split("UserID");
    System.out.println("count :" +j);
    audit = auditArray[1];

    for (int i = 1; i < columnlist.length; i++) {
        auditArray = audit.split(columnlist[i]);
        balle();
    }


    final_log[j]= audit.trim().substring(1).trim();

    for (int i = 0; i < final_log.length; i++) {
        System.out.println("test : " +final_log[i]);
    }



    }

    public static void balle(){
        final_log[j] = auditArray[0].trim().substring(1).trim();
        audit = auditArray[1];
        System.out.println(final_log[j]);
        j++;
    }

}

log1 的控制台输出是::

count :0
ccmadministrator
172.30.235.29
5
GeneralConfigurationUpdate
CUCMAdmin
Success
No
AdministrativeEvent
Cisco CUCM Administration
record in table batjob with key field name = Export Configuration, Job id : 1380812040 added
Cisco Tomcat

test : ccmadministrator
test : 172.30.235.29
test : 5
test : GeneralConfigurationUpdate
test : CUCMAdmin
test : Success
test : No
test : AdministrativeEvent
test : Cisco CUCM Administration
test : record in table batjob with key field name = Export Configuration, Job id : 1380812040 added
test : Cisco Tomcat
test : 
test : iptapps-eft-cucm1

log2 的控制台输出是::

count :0
ccmadministrator
172.30.238.14
6
GeneralConfigurationUpdate
Cisco CCM Serviceability RTMT
Success
No
AdministrativeEvent
Cisco CCM Serviceability RTMT
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
    at test.balle(test.java:43)
    at test.main(test.java:27)

来自 OP 的评论

Line 43 is :: audit = auditArray[1]; {present in balle method}     
4

2 回答 2

7

问题是,当您使用列名重复拆分字符串时,您无意中截断了字符串的其余部分。

发生的事情log2是“严重性”在字符串中出现了两次,所以结果split是一个三元素数组。然后,您只继续处理索引为 1 的元素中的内容。因此,您将丢弃第二次出现“严重性”之后的内容。

当您在循环中开始查找字符串“App ID”时,您无法在字符串中找到它,因为它位于第二个“Severity”之后。因此split只返回一个元素,并且您获得了您获得的异常。

要解决此问题,您应该使用第二个参数 ofsplit来限制该方法返回的令牌数量。代替 :

auditArray = audit.split(columnlist[i]);

利用

auditArray = audit.split(columnlist[i], 2);
于 2013-10-16T17:48:11.800 回答
0

兄弟,您正在使用的日志 2 包含以下字符串。“思科系统日志代理:SYSAGENT:SyslogSeverityMatchFound”。此字符串会产生问题。由于此字符串,拆分返回的数字比您的数组预期的多。只需正确使用它,您的问题就会解决。

希望这对你有帮助。

于 2013-10-16T18:06:54.650 回答