cancel
Showing results for 
Search instead for 
Did you mean: 

Handle Strings in UDF

Former Member
0 Kudos

Hi Guys,

I have a UDF in my Mapping and in this function I don't know how to read String values out the Inbound Context.

When I write for example

public void HandleString(String[] var1, String[] var2, ResultList result, ResultList result2, ResultList result3, Container container) throws StreamTransformationException{

int pos = var1[0].indexOf("");* there occurs an exception: Exception:java.lang.ArrayIndexOutOfBoundsException: 0

Can you please tell me what I did wrong?.

Thanks already.

Best Regards

Martin

Accepted Solutions (1)

Accepted Solutions (1)

markangelo_dihiansan
Active Contributor
0 Kudos

Hello,

Instead of creating your UDF inside the definitions tab in message mapping, try creating it in the functions tab.

Hope this helps,

mark

Former Member
0 Kudos

Hi Mark,

sorry for not telling this information. That is what I am doing.... I write my coding in the functions-tab.

Martin

markangelo_dihiansan
Active Contributor
0 Kudos

Hello,

int pos = var1[0].indexOf("");*

Maybe the reason for the error is that you don't have any blank values in var1, that is why when you pass it to an array, it is giving an out of bound exception because pos has a value of -1.

Hope this helps,

Mark

Former Member
0 Kudos

I look for "*" in my String, not for Blank.... my mistake. Sorry.

var1 is one Inbound parameter and in the queue is only one value. But don't know how to read this value.

Converting to Integer doesn't work

RKothari
Contributor
0 Kudos

Try the below code:

String s = var1[0].toString();

int pos = s.indexOf('*');

Former Member
0 Kudos

Hi Rahul,

thanks for your Reply. But the error still exists.

String s = var1[0].toString();

int pos = s.indexOf('*');

int pos1 = 0;

String sub2 = null;

int length = s.length()-1;

String zspeich1 = null;

String zspeich2 = null;

while(pos != (length-1)){

zspeich1 = s.substring(0, pos);

result.addValue(zspeich1);

pos1 = s.indexOf("*", pos+1);

if (pos1>0)

zspeich2 = s.substring(pos+1, pos1);

result2.addValue(zspeich2);

pos = pos1;

if (pos == (length-1)){

zspeich2 = s.substring(length, length+1);

result2.addValue(zspeich2);

}

}

Here my whole coding. everywhere I use a String method I inserted the "s".

Former Member
0 Kudos

try this

if (var1.length > 0){

//put in here all your code

}

else return "";

Former Member
0 Kudos

Now it works!!

Thank you very much ciochinah ...

Best Regards

Martin

markangelo_dihiansan
Active Contributor
0 Kudos

Hello Martin,

String sub2 = null;

int length = s.length()-1;

String zspeich1 = null;

String zspeich2 = null;

Can you try using


String sub2 = "";
String zspeich1 = "";
String zspeich2 = "";

Hope this helps,

Mark

Former Member
0 Kudos

glad to help

Answers (2)

Answers (2)

Former Member
0 Kudos

Hi Martin, This is incorrect format: >> Public void HandleString(String ] var1, String[ var2, ResultList result, ResultList result2, ResultList result3, Container container) throws StreamTransformationException The Square brackets are incorrect to be present in the statement this way... Is this correct in your PI function library? Regards, Nipun

Former Member
0 Kudos

Hi,

You have to convert input String to integer.

Use the following code:

int pos = Integer.parseInt(var1);

or

int pos = Integer.valueOf(var1);

Edited by: Puneet Singhal on Mar 4, 2011 11:02 AM

Edited by: Puneet Singhal on Mar 4, 2011 11:03 AM