Skip to Content
0
Former Member
Apr 10, 2013 at 10:09 AM

UDF Code Error: String index out of range

263 Views

Hi PI experts,

I have a UDF code below which resulted to error "String index out of range" (note "a" is filename from DynamicConfiguration):

String[] b=a.split("_");
String ret=null;
String ret1=null;
int i;

if (a.startsWith("AB"))
{
i = a.indexOf('.');
i = i-2;
if (a.substring(i,a.indexOf('.')).equals("U"))
{
ret1=b[4].concat("_").concat(b[5]);
}
else
{
ret=b[4].concat("_").concat(b[5]);
ret1=ret.substring(0,ret.indexOf('.'));
}
}
else
{
ret=b[4].concat("_").concat(b[5]);
ret1=ret.substring(0,ret.indexOf('.'));
}

return ret1;

The requirement is to identify if the filename has "_U" before the extension ".txt" for filenames that begin with "AB". Then the value of XXXX_XXX should be returned by the function. Split on "_" is used to store filename in an array.

Sample applicable filenames in the system:

ABBB_CoCd_AAAA_yyyymmdd_XXXX_XXX.txt

ABBB_CoCd_AAAA_yyyymmdd_XXXX_XXX_U.txt

BBBB_CoCd_AAAA_yyyymmdd_XXXX_XXX.txt

The line a.startsWith("AB") is working, but I think the error is in the line "if (a.substring(i,a.indexOf('.')).equals("U"))" though I cannot prove or debug how it happened. May I ask for your help to identify which code is wrong?

Thanks!