cancel
Showing results for 
Search instead for 
Did you mean: 

Need help to Create UDF

Former Member
0 Kudos

Hi All,

I have a requirement as below:

The Input field is coming as 20_30 or 2_40 ...But i need value only Before "_" adn if it is a single digit then it should add 0 at the starting.

For 20_30 -> 20

For 2_30 -> 02

I have written one UDf,but it is not giving me the first part before "_".

Pleas help.

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi,

If you are using simple UDF (sending only one input value) then use this code

int ind=inputVal.indexOf('_'); //inputVal is the input variable name

return inputVal.substring(0,ind);

If you are using Queue/Context type UDF (sending array of input values) then use the below code

for(int i=0; i<inputVal.length; i++)

{

int ind=inputVal<i>.indexOf('_');

result.addValue(inputVal<i>.substring(0,ind);

}

Regards,

Venkata Ramesh

Former Member
0 Kudos

Both of them not working....

Former Member
0 Kudos

Check this code

int len= inputVar.indexOf("_");

if (len >= 0)

return inputVar.substring(0, len));

else

return inputStr;

Regards,

Venkata Ramesh

Former Member
0 Kudos

cannot find symbol symbol :

method indexOf(java.lang.String)

location: class java.lang.String[] int ind=var1.indexOf("_");

^

Function check, Line 4:

cannot find symbol symbol :

method substring(int,int)

location: class java.lang.String[] result.addValue(var1.substring(0,ind));

Former Member
0 Kudos

Hi Vankata,

For this code:

int len= input.indexOf("_");

if (len >= 0)

return input.substring(0, len);

else

return input;

I am getting these errors:

Function check, Line 1:

cannot find symbol symbol : method indexOf(java.lang.String) location: class java.lang.String[] int len= input.indexOf("_"); ^

Function check, Line 3:

cannot return a value from method whose result type is void return input.substring(0, len); ^

Function check, Line 5:

cannot return a value from method whose result type is void return input; ^

Note: /usr/sap/XID/DVEBMGS30/j2ee/cluster/server0/./temp/classpath_resolver/Map2448bffc61ee11e1c1fb00001254b94e/source/com/sap/xi/tf/_MM_G_MDM_Product_ODS_1404_.java uses or overrides a deprecated API. Note: Recompile with -Xlint:deprecation for details. 3 errors

former_member184681
Active Contributor
0 Kudos

How about the code that I provided above? It is already tested and I am sure it will cause no errors.

Former Member
0 Kudos

Hi Greg,

I used your code and still its giving me this error

Function check, Line 1:

cannot find symbol symbol : method indexOf(char) location: class java.lang.String[] String result1 = input.substring(0, input.indexOf('_')); ^

Function check, Line 2:

incompatible types found : java.lang.String required: com.sap.aii.mappingtool.tf7.rt.ResultList if (result1.length() == 1) result = "0" + result1; ^

Function check, Line 3:

cannot return a value from method whose result type is void return result; ^ Note: /usr/sap/XID/DVEBMGS30/j2ee/cluster/server0/./temp/classpath_resolver/Map28f10a8361f011e18e8400001254b94e/source/com/sap/xi/tf/_MM_G_MDM_Product_ODS_1404_.java uses or overrides a deprecated API. Note: Recompile with -Xlint:deprecation for details. 3 errors

Former Member
0 Kudos

Hi,

Use the below code

create simple type UDF and input var name is inputStr

int len= inputStr.indexOf("_"); //O is capital letter here

if (len >= 0)

{

String temp = "";

temp= inputStr.substring(0, (len)); //in substring all are small letters

return temp;

}

else

return inputStr;

former_member184681
Active Contributor
0 Kudos

Ok, looks like my code does not match your Execution Type You can either change the Execution Type for your UDF to Single Values, or change the code to the following:


for(int i=0; i<input.length; i++)
{
String result1 = input<i>.substring(0, input<i>.indexOf('_'));
if (result1.length() == 1) result1 = "0" + result1;
result.addValue(result1);
}

Hope this helps,

Greg

Former Member
0 Kudos

Hi Grez,

The code you provided is working fine....

Thanks a lot...

Now, 20_30 => 20

2_30 => 02

But if I am sending only 20, then its giving error...what addition should i do in UDF to accommodate this condition also?????

Former Member
0 Kudos

> But if I am sending only 20, then its giving error...what addition should i do in UDF to accommodate this condition also?????

int ind=0;

for(int i=0; i<inputVal.length; i++)

{

ind=inputVal<i>.indexOf('_');

if (ind > 0)

result.addValue(inputVal<i>.substring(0,ind));

else result.addValue(inputVal<i>);

}

former_member184681
Active Contributor
0 Kudos

Actually it depends on what do you want to do with such "20" value. Should it be interpreted as 20, or 20? In the first case, use the following code:

for(int i=0; i<input.length; i++) {
   int underscorePosition = input<i>.indexOf('_');
   if (underscorePosition > 0) {
      String result1 = input<i>.substring(0, );
      if (result1.length() == 1) result1 = "0" + result1;
      result.addValue(result1);
   }
   else result.addValue("");
}

For the second case (20_), use

else result.addValue(input<i>);

instead of

else result.addValue("");

Hope this helps,

Greg

Former Member
0 Kudos

Hi,

20 should not be interpreted as any of 20 or 20.

Rather it should be taken only as 20 without "_".

How to deal with it....????

former_member184681
Active Contributor
0 Kudos

Ok, maybe I wasn't precise enough. When the input is 20, do you want the output to be 20 also, or to be empty?

Former Member
0 Kudos

To be very precise, I want the following input - output combinations :

Input - 20_30

Output - 20

Input - 2_30

Output - 02

Input - _30

Output - Blank

Input - 200_30

Output - 20

Input - Blank

Output - Blank

Input - 20_

Output - 20

former_member184681
Active Contributor
0 Kudos

Ok, then this should solve it for you finally:

for(int i=0; i<input.length; i++) {
   int underscorePosition = input<i>.indexOf('_');
   if (underscorePosition > 0) {
      String result1 = input<i>.substring(0, underscorePosition);
      if (result1.length() > 2) result1 = result1.substring(0, 2);
      if (result1.length() == 1) result1 = "0" + result1;
      result.addValue(result1);
   }
   else result.addValue("");
}

Former Member
0 Kudos

hey Sorry...Everything is working fine...

But please add one more condition :

Input - 20

Output - 20

former_member184681
Active Contributor
0 Kudos

Ok, this should work finally:

for(int i=0; i<input.length; i++) {
   int underscorePosition = input<i>.indexOf('_');
   if (underscorePosition > 0) {
      String result1 = input<i>.substring(0, underscorePosition);
      if (result1.length() > 2) result1 = result1.substring(0, 2);
      if (result1.length() == 1) result1 = "0" + result1;
      result.addValue(result1);
   }
   else result.addValue(input<i>);
}

Former Member
0 Kudos

I already tried this answer...

But its giving this Output when Sending 20 as Input -> [Ljava.lang.String;@550b5e33

former_member184681
Active Contributor
0 Kudos

That's strange, honestly. You can try a little workaround, maybe this will help:

else {
String result2 = input<i>;
result.addValue(result2); }

Former Member
0 Kudos

Thanks a ton...It works very well with all the conditions now....:)

Answers (1)

Answers (1)

Former Member
0 Kudos

chk this:

input: var1


String [] temp = var1[0].split("_");
result.addValue(temp[0]);

mapping:

var1-udf-formatnumber(00)---target

Former Member
0 Kudos

Hi,

Its giving the error - Cannot cast '' to decimal number.

Any other change???????

Former Member
0 Kudos

whats the input u r passing??

and where exactly u are getting this error??use display queue and chk ur mapping output

former_member184681
Active Contributor
0 Kudos

Hi,

Try with the following, I already tested it and it works fine:

String result = input.substring(0, input.indexOf('_'));
if (result.length() == 1) result = "0" + result;
return result;

Hope this helps,

Greg