我正在尝试解析字符串并将其存储在字符串数组中。当我试图解析字符串“log1”时,我能够解析它。 但是当我解析字符串“log2”时,得到这个“线程中的异常”主“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}
答案 0 :(得分:7)
问题在于,当您使用列名重复拆分字符串时,会无意中截断字符串的其余部分。
log2
发生的事情是“严重性”在字符串中出现两次,因此split
的结果是一个三元素数组。然后,您只继续索引为1的元素中的内容。因此,您将丢弃第二次出现“严重性”后的内容。
当你在循环中开始寻找字符串“App ID”时,你就无法在字符串中找到它,因为它是在第二个“Severity”之后。因此split
只返回一个元素,并获得您获得的异常。
要解决此问题,您应该使用split
的第二个参数来限制方法返回的标记数。而不是:
auditArray = audit.split(columnlist[i]);
使用
auditArray = audit.split(columnlist[i], 2);
答案 1 :(得分:0)
您使用的日志2兄弟包含以下字符串。 “Cisco Syslog代理:SYSAGENT:SyslogSeverityMatchFound”。此字符串会产生问题。由于此字符串,拆分返回的数字比您的数组期望的数量多。只需正确使用它,你的问题就会解决。
希望这对你有所帮助。