cancel
Showing results for 
Search instead for 
Did you mean: 

Index Out of bound Exception

Former Member
0 Kudos

Hi Experts,

I have return UDF. but for me it is giving the index out of bound exception.

From Source field i need last 4 characters needs to populate in the target field.

public String ModelCode(String SGTXT,Container container){

int len=0;

int len1=0;

String result=" ";

len= SGTXT.length( );

len1 = len-4;

result = SGTXT.substring(len1,len);

return result;

}

cuold you please help me out.

Regards,

Prabhakar.A

Accepted Solutions (0)

Answers (2)

Answers (2)

baskar_gopalakrishnan2
Active Contributor
0 Kudos

Try this to avoid exception....

public String ModelCode(String SGTXT,Container container){
int length = SGTXT.length();
if(length < 4){
    return "";
}
return SGTXT.substring(length-4);
}

Former Member
0 Kudos

In your code check if len GE 4 then only disply the results. What is happening now, whenever the length is less than 4 "substring" throws an exception. So use below code..

public String ModelCode(String SGTXT,Container container){
 int len=0;
 int len1=0;
 
 String res =" ";
 len= SGTXT.length( );
 
if(len GE 4)
{
   len1 = len-4;
   res = SGTXT.substring(len1,len);
 }
   return res;
}

Former Member
0 Kudos

HI Friends,

always the SGTXT value varies.

For Example SGTXT is ABCD12345678909876 the output should be 9876. Only Last 4 characters should be populate.

Regards,

Prabhakar.A

baskar_gopalakrishnan2
Active Contributor
0 Kudos

Please test my code with various length of input string and let me know. it should work