cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Mapping issue: Function 'Substring error'

Former Member
0 Likes
2,272

Hi All,

I have an incoming field of length upto 30 which needs to be split into 3 diff fields of length 10 each.

I used the function 'substring' in Mapping using the following range 0-10 and 10-20 and 20-30. It works fine if the string has all 30 characters.

If the string has only 10 characters, then the first field is poulated but for the remiaing 2 fields I get the error

Exception:[java.lang.StringIndexOutOfBoundsException: String index out of range: 10] in class com.sap.aii.mappingtool.flib3.TextFunctions method substring[185120, com.sap.aii.mappingtool.tf3.rt.Context@3d1048df]

I guess the issue is because string is only 20 caharcters long.

Is there any other way I can split the string without getting this error.

Many thanks

Shirin

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Likes

Hi,

Use length function for actual length if it is less than the length make your own logic.

Thanks,

RamuV

Former Member
0 Likes

Hi,

I do not know java. Can you help let me know a sample UDF to code this.

All I need is a UDF with 2 incoming parameters and 1 outbound.

Incoming:

1. for the data string.

2. Field no ( e.g 1 or 2 or 3)

Outbound:

1. Value of the substring

Logic would be like.

1. Read incoming string. If data found proceeed else error

2. Based on 2nd incoming filed determine whcih data string to be found. e.g if 2nd field = 2, then I need string 10 to 20. If 2nd field is 3, then i need data string 20 to 30 and so on.

Apprciate your help.

Many thanks

Shirin

GabrielSagaya
Active Contributor
0 Likes

function myudf(String a, String b, Container container)

{

int num=0;

int times=0;

int start=0;

try

{

num=Integer.parseInt(b);

times=num*10;

if (num>1)

start=(num-1)*10;

else

start=0;

if (a.length>times)

return a.subString(start,10);

else

return "No String found";

}catch(NumberFormatException nfe)

{

return "Exception occured"+e;

}

}

Former Member
0 Likes

Thanks Gabriel,

Am trying to use it but am getting the following errors

Source code has syntax error:  D:/usr/sap/XRD/DVEBMGS02/j2ee/cluster/server0/./temp/classpath_resolver/Map7884ba305ca011ddcaaa001a4b52813a/source/com/sap/xi/tf/_MM_CONFIGURATION_.java:1168: 'class' or 'interface' expected public static void main(String[] args) throws Exception{/*!_$ClNRep_*/_MM_CONFIGURATION_ st = new /*!_$ClNRep_*/_MM_CONFIGURATION_(); st.testExecute(); } ^ D:/usr/sap/XRD/DVEBMGS02/j2ee/cluster/server0/./temp/classpath_resolver/Map7884ba305ca011ddcaaa001a4b52813a/source/com/sap/xi/tf/_MM_CONFIGURATION_.java:1169: 'class' or 'interface' expected

Thanks

Shirin

Santhosh_Vellingiri
Active Contributor
0 Likes

Hi,

Try out this UDF..

public String test(String a,String b,Container container){
String err = "Error String";
 int val=Integer.parseInt(b);
if(val==1)
{
if(a.length()>10)
return a.substring(0,10);
else
return a.substring(0,a.length());
}
else if(val==2)
{
if(a.length()<10)
return err;
else if(a.length()>20)
return a.substring(10,20);
else
return a.substring(10,a.length());
}
return err;
}

Thanks

SaNv...

Former Member
0 Likes

Hi Santosh,

Thanks for the code.

I have copied/pasted ur code but upon activation I get the error:

Source code has syntax error:  D:/usr/sap/XRD/DVEBMGS02/j2ee/cluster/server0/./temp/classpath_resolver/Map442ab5d05ca211dd81f7001a4b52813a/source/com/sap/xi/tf/_MM_CONFIGURATION_.java:1166: 'class' or 'interface' expected public static void main(String[] args) throws Exception{/*!_$ClNRep_*/_MM_CONFIGURATION_ st = new /*!_$ClNRep_*/_MM_CONFIGURATION_(); st.testExecute(); } ^ D:/usr/sap/XRD/DVEBMGS02/j2ee/cluster/server0/./temp/classpath_resolver/Map442ab5d05ca211dd81f7001a4b52813a/source/com/sap/xi/tf/_MM_CONFIGURATION_.java:1167: 'class' or 'interface' expected } ^ D:/usr/sap/XRD/DVEBMGS02/j2ee/cluster/server0/./temp/classpath_resolver/Map442ab5d05ca211dd81f7001a4b52813a/source/com/sap/xi/tf/_MM_CONFIGURATION_.java:1168: 'class' or 'interface' expected

Any suggestion what might be worong

Thanks

Shirin

Answers (5)

Answers (5)

MichaelSargent
Participant
0 Likes

To show graphical mapping to avoid substring exception error. Response is a string with varied length. The logic below tests the string to check if it is greater than 50. If it is, a substring is used to get from 50 to 100, else a blank constant is used to fill STAPA2.

Former Member
0 Likes

Hi shirin,

Check with the braces.

Santhosh_Vellingiri
Active Contributor
0 Likes

Hi,

Use the combination of length, if and substring function in the graphical mapping

Thanks

SaNv...

Former Member
0 Likes

Hi,

this is the property of substring function if the value is less then the defined value it will through an error.

if you are felxible with having value in only one part e.g. 10 char then go for UDF. just write an UDF and inside udf code you can take values based on incoming value length.

Regards,

Sarvesh

Former Member
0 Likes

Hi Shrin,

best way is to write UDF (Java) . get the length of the String and based upon the length u can write u r code to get the string first three or watever u requirment is demanding

rgds

srini