on 04-10-2013 11:09 AM
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!
Hi,
substring() is always riscy coz the string might be too short. If you use it, ask before for the length of the string like
int index;
index = ....
if (a.length >= index)
substring ...
else
do something different
/Udo
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Udo,
Thanks for the feedback. I'm not really familiar with UDF Java programming, but I found a different way to do the requirement and will share it also:
Code:
String u = a.substring(0,a.indexOf('.')); <- this is to remove ".txt"
if (u.startsWith("AB") && u.endsWith("U")) <- check if the string starts with AB and ends with U
I provided points to you.
Thanks!
User | Count |
---|---|
93 | |
10 | |
10 | |
9 | |
9 | |
7 | |
6 | |
5 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.