Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting ArrayIndexOutOfBound : 1 exception

I am trying to parse a string and store it in a string array. When i am trying to parse string "log1", i am able to parse it. But when i am parsing string "log2" , getting this "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1".

Running the below 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++;
    }

}

Console output in case 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

Console output in case 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)

FROM OP's comment:

Line 43 is :: audit = auditArray[1]; {present in balle method}     
like image 455
Vivone Avatar asked Feb 01 '26 00:02

Vivone


1 Answers

The problem is that when you are splitting a string repeatedly with the column names, you are unintentionally truncating the rest of your string.

What is going on with log2 is that "Severity" is present twice in the string, so the result of split is a three-elements array. Then you only continue with what is in the element whose index is 1. Therefore you are discarding what is after the second occurrence of "Severity".

When in your loop you start looking for the string "App ID", then, you can not find it in the string, because it was after the second "Severity". Therefore split only returns one element, and you obtain the exception you obtain.

To fix that, you should use second parameter of split to limit the number of tokens returned by the method. Instead of :

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

use

auditArray = audit.split(columnlist[i], 2);
like image 73
Cyrille Ka Avatar answered Feb 02 '26 15:02

Cyrille Ka



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!