我正在尝试解析一个字符串并将其存储在一个字符串数组中。当我试图解析字符串“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}