on 02-28-2012 8:27 AM
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.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
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
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
> 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>);
}
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
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("");
}
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>);
}
chk this:
input: var1
String [] temp = var1[0].split("_");
result.addValue(temp[0]);
mapping:
var1-udf-formatnumber(00)---target
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
87 | |
10 | |
10 | |
9 | |
7 | |
7 | |
6 | |
5 | |
4 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.