cancel
Showing results for 
Search instead for 
Did you mean: 

UDF to Check the Value or not

Former Member
0 Kudos

Hi

I have to write the UDF to check the value exists or not ,, if exists then i need to take the values before ,

Ex : TEST,DEMO

then the out put is TEST

if the value does not exists then pass the empty value

S V

Accepted Solutions (0)

Answers (4)

Answers (4)

Former Member
0 Kudos

String s = ",",c=null;
int len;
int i = var1.indexOf(s);

len = var1.length();
if (len == 0)
{
 c = " ";
return c;
}
 else if(i>-1)
c = var1.substring(0, i);
return c;

Now u dont get the Exception

because u earlier did not check the condition before getting the substring whether it found the ", "or not if it does not find it then u will get the i value as -1 and if u try to get the substring u get that exception

Initialize the value in the variable c (the value u want to return)if it does not find comma

Shabarish_Nair
Active Contributor
0 Kudos

your logic should be as follows;

                                                                          THEN ---------> input field --------> TARGET
input fields -------------> EXISITS (node function) -> IF
                                                                         ELSE ------> substring of inputfield (0,indexOf(,))

Former Member
0 Kudos

HI

I wrote following UDF :

String s = ",";

int len;

int i = var1.indexOf(s);

i++;

len = var1.length();

if (len == 0)

{

String c = " ";

};

String c = var1.substring(0, i-1);

return c;

still its giving me error while testing the mapping

Runtime exception when processing target-field mapping /ZPAT/IODCZE1I/CITY; root message: Exception:[java.lang.StringIndexOutOfBoundsException: String index out of range: -1] in class

justin_santhanam
Active Contributor
0 Kudos

SV,

What are you trying to achieve? Could you please give an example?

Thanks,

raj.

siddhesh_pathak4
Contributor
0 Kudos

Hey SV,

try to handle the exception stringIndexOutofBoundException.

Put your code under try catch block and handle the exception in catch.

try{

// your code

}catch(StringIndexOutOfBoundsException){};

Former Member
0 Kudos

Hi

My requirement is ..

On source side i have one field ctyst and which has a value Atlanta, GA

i have to map Atlanta to one field and GA to another fiels on target.. basically i have to separate the values before and after comma..

i able to achieve that .. my problem is when there is no value in Ctyst field .. its giving error in Mapping .. how i will handle that ..

my UDF code:

String s = ",";

int len;

int i = var1.indexOf(s);

i++;

len = var1.length();

if (len == 0)

{

String c = " ";

};

String c = var1.substring(0, i-1);

return c;

justin_santhanam
Active Contributor
0 Kudos

SV,

Can you try the below code?

UDF

The UDF must have two inputs. catyst , const1



String return_field1=" ";
String return_field2 =" ";
StringTokenizer st = new StringTokenizer(catyst,",");

while(st.hasMoreTokens())
{
return_field1 = st.nextToken();
return_field2 = st.nextToken();
}

if(const1.equals("field1"))
return return_field1;
else
return return_field2;

The above code can be used for both of your target fields. If the source is blank then blank values will be returned to the target fields.

The mapping logic must be

catyst &CONSTANT("field1") ---> UDF --->Target field 1

catyst & CONSTANT("field2") ---> UDF ---> Target field2

Thanks,

raj.

siddhesh_pathak4
Contributor
0 Kudos

hello,

did you tried with try catch block. as suggested in previous block..you can handle tat exception using try catch..

String s = ",";
int len;
int i = var1.indexOf(s);
i++;
len = var1.length();
try {
if (len == 0)
{
String c = " ";
};

String c = var1.substring(0, i-1); 
return c;
}catch(StringIndexOutOfBoundsException e) {};

Former Member
0 Kudos

Hi

If that is the case u can use MapWithDefault Node Function , it will send if any value present , set default value in it as "Space", and this gets triggers if source has no value.

rgds

srini

justin_santhanam
Active Contributor
0 Kudos

SV,

if for example you have Test1, Demo1, Demo2 .. then still you have to return Test1?

Thanks,

raj.