Java – get arrayindexoutofbound: 1 exception
I try to parse a string and store it in a string array When I try to parse the string "Log1", I can parse it
Run the following code:
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++; } }
The console output of Log1 is:
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
The console output of log2 is:
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)
Comments from OP:
Line 43 is :: audit = auditArray[1]; {present in balle method}
Solution
The problem is that when you repeatedly split a string with a column name, you inadvertently truncate the rest of the string
The function of log2 is that "severity" appears twice in the string, so the result of split is a three element array Then, you continue with only the contents of the element with index 1 Therefore, you will discard the content after the second occurrence of severity
In your loop, you start looking for the string "app ID", and then you can't find it in the string because it is after the second "severity" Therefore, split returns only one element and gets the exception you get
To solve this problem, you should use the second parameter of split to limit the number of tags returned by the method Substitute:
auditArray = audit.split(columnlist[i]);
use
auditArray = audit.split(columnlist[i],2);