cancel
Showing results for 
Search instead for 
Did you mean: 

How to find length of integer elements using message mapping

Former Member
0 Kudos

Hi all,

How can I find the number of digits in an element using message mapping

XIer

Accepted Solutions (1)

Accepted Solutions (1)

henrique_pinto
Active Contributor
0 Kudos

All input fields are considered as strings.

Thus you can use the Length standard text field to check its length.

Be aware that it will consider even the dot, comma and possible non-significant zeros.

Regards,

Henrique.

Former Member
0 Kudos

Henrique,

I have a scenario wherein I need to check the length of the incoming field,

example

if field1 has a lenght of 5

than if the incoming field does not exist than I should fill 5 spaces in field1

if the incoming field contains 2 chars, i need to fill it with the 2 char followed by 3 spaces.

How do I go about doing this?

XIer

henrique_pinto
Active Contributor
0 Kudos

use UDF for that.

Regards,

Henrique.

Former Member
0 Kudos

Could you tell me how to do that, I am not that good with Java

Thanks,

XIer

justin_santhanam
Active Contributor
0 Kudos

Hello,

<b>Try the below code</b> Input -argument name :st

StringBuffer sb= new StringBuffer();

if(st.length()>=5)

{

return""st"";

}

else

{

for(int i=st.length();i<=5;i++)

{

if(i==st.length())

sb.append(st);

else

sb.append(" ");

}

return ""sb.toString()"";

}

Best regards,

raj.

Former Member
0 Kudos

Will I have to change the value '5' for each and every variable?

I have 100 elements in the structure.

XIer

Former Member
0 Kudos

So if you have 100 elements you are going to create 100 UDFs? If yes then performance may hit. Please reconsider the design.

Regards,

---Satish

Former Member
0 Kudos

What are the + signs in the code and double quotes?

XIer

Former Member
0 Kudos

Satish, yes the code is working fine. Thanks a lot for your help.

However could you pls tell me why you have you '+' signs before and after the string and why have you used double double quotes?

XIer

Message was edited by:

XIer

Former Member
0 Kudos

Hi,

++ is used to increment the value by 1 and "" is nothing but constant space.

Regards,

---Satish

Former Member
0 Kudos

Satish,

Cause I have to write the code for 100 elements, I have decided to have a AUDF with two inputs;

1) for the element

2) for the length of the data type (I am passing length using Constant message mapping func).

Now when I change my UDF and try running it I get the following error:

<b><i>Start of test

Source code has syntax error: /usr/sap/B06/DVEBMGS00/j2ee/cluster/server0/./temp/classpath_resolver/Map0999e66045cf11dcc83b4ebcfc11da82/source/com/sap/xi/tf/_MM_Recordcount1_.java:160: operator >= cannot be applied to int,java.lang.String if(Element_Name.length()>=Element_Length) ^ /usr/sap/B06/DVEBMGS00/j2ee/cluster/server0/./temp/classpath_resolver/Map0999e66045cf11dcc83b4ebcfc11da82/source/com/sap/xi/tf/_MM_Recordcount1_.java:166: operator <= cannot be applied to int,java.lang.String for(int i=Element_Name.length();i<=Element_Length;i++) ^ 2 errors</i></b>

The code I have used is:

public String Include_Space(String Element_Name,String Element_Length,Container container)

{StringBuffer sb= new StringBuffer();

if(Element_Name.length()>=Element_Length)

{

return""Element_Name"";

}

else

{

for(int i=Element_Name.length();i<=Element_Length;i++)

{

if(i==Element_Name.length())

sb.append(Element_Name);

else

sb.append(" ");

}

return ""sb.toString()"";

}

Pls advice, where I am going wrong...

XIer

Message was edited by:

XIer

justin_santhanam
Active Contributor
0 Kudos

Hi,

if(Element_Name.length()>=Element_Length)

In the above code where u are getting Element_Length value from?

Best regards,

raj.

Former Member
0 Kudos

for the length of the data type (I am passing length using Constant message mapping func).

XIer

justin_santhanam
Active Contributor
0 Kudos

public String Include_Space(String Element_Name,String Element_Length,Container container)

{

int len = Integer.parseInt(Element_Length);

StringBuffer sb= new StringBuffer();

if(Element_Name.length()>=len)

{

return""Element_Name"";

}

else

{

for(int i=Element_Name.length();i<=len;i++)

{

if(i==Element_Name.length())

sb.append(Element_Name);

else

sb.append(" ");

}

return ""sb.toString()"";

}

The problem is u have to typecast the Element_Length to int. U Can't compare

Element_Length() >= Element_Length

<b>int</b> >= <b>String</b>

Hope it clears!!

Try the above code and let us know the results.

Best regards,

raj.

Former Member
0 Kudos

Thanks Raj it's working fine now.

XIer

justin_santhanam
Active Contributor
0 Kudos

U welcome!!

Best regards,

raj.

Answers (2)

Answers (2)

Former Member
0 Kudos

oh lol at this, we have this function in use too, mine may or may not be more simple, the pad character would be a space in your case:

input variables, (String inString, Desired length inLength, Pad Character inPadChar)

public String FillLeft(String inString,String inLength,String inPadChar, Container containeer)

int tLength = Integer.parseInt(inLength);//conversion of string to integer

int aLength;

/**

*Returns an exception if the padd character is more than one letter

*/

if(inPadChar.length()>1)

{

return "EXCEPTION 1 IN XI MAPPING, UDF FUNCTION- FILLLEFT";

}

/**

*Returns an exception if the string is longer than the desired return string's length

*/

if(inString.length()>tLength)

{

return "EXCEPTION 2 IN XI MAPPING, UDF FUNCTION- FILLLeft";

}

/**

/* actual padding of the String

*/

aLength = tLength-inString.length();

for(int i=0;i<aLength;i++)

{

inString=inPadChar+ inString;

}

return inString;

Former Member
0 Kudos

Could you tell me why ..what you want to exact by finding the length ...