cancel
Showing results for 
Search instead for 
Did you mean: 

Java in XI

Former Member
0 Kudos

Using a UDF simple or advanced... you can only have input type string and output type string... Is this true?

So if this is true, then how can you make custom calculations...

can you have a string input which is number i.e. 50

and then an output as a string but numeric i.e. 80

and integer calculations inside. So you convert string to integer... do calculations, get the answer then convert the answer to a string?

Basically I am comfortable doing comparisons of strings but I would like to do scennarios which include integers and calculations.... or is this not possible in XI UDF's?

Message was edited by:

Alex Ong

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Now I have changed it to this:

int one;

int two;

int result = one+two;

if (a.equals(b))

{one = 20; two = 30;}

else

{one = 50; two = 50;}

return result.toString();

And I get error:

16:04:22 Start of test

Source code has syntax error:

/usr/sap/CXD/DVEBMGS00/j2ee/cluster/server0/./temp/classpath_resolver/Map9bd049d0860c11dbc783000d60de069c/source/com/sap/xi/tf/_MM_Source_Target_.java:43: int cannot be dereferenced

return result.toString();

^

1 error

Former Member
0 Kudos

Hi Alex,

the problem here is in

int result = one+two;

..

return result.toString();

<b>int</b> is a basic type and not an object, so it does not have the method toString(), it has no method.

<b>Integer</b> instead is an Object, defined by Sun to "support" the basic type int

then if you want to convert an int to a String :

return new Integer(intValue).toString();

Hope it solve your doubts!

Sergio

Answers (15)

Answers (15)

Former Member
0 Kudos

Sergio...

Your solution worked perfctly... (As Usual...)

Are you a Java Programmer who moved into XI or XI who learnt Java?

Former Member
0 Kudos

Hi Alex,

thanks, I'm happy it works!!

You discover it, I'm a Java programmer who moved to XI (but still loving so much Java )

Kind Regards,

Sergio

Former Member
0 Kudos

int cannot be dereferenced

return result.toString();

^

That doesnt mention my else statement... does it?

it is saying that int cannot be dereferenced... what does that mean?

Can someone please let me know what this is?

bhavesh_kantilal
Active Contributor
0 Kudos

Alex,

Can you try this,


String newResult = result.toString();
return newResult;

Regards,

Bhavesh

Former Member
0 Kudos

Hi

Try like this for simple function

int one;

int two;

if (a.equals(b)){

one = 20;

two = 30;

}

else{

one = 50;

two = 50;

}

int result = one+two;

return ""+result;

Regards,

Sai

Former Member
0 Kudos

int cannot be dereferenced

return result.toString();

^

That doesnt mention my else statement... does it?

it is saying that int cannot be dereferenced... what does that mean?

Former Member
0 Kudos

Hey Michal,

Sorry it returned the above error.

This is why I am confused because I have been trying different variations and most of the time I get that error message?

Why what does it mean?

MichalKrawczyk
Active Contributor
0 Kudos

hi,

that you else statement is not ok

Regards,

michal

Former Member
0 Kudos

Hi Michal,

Thanks I tried it and I got the same error as above

16:06:17 Start of test

Source code has syntax error:

/usr/sap/CXD/DVEBMGS00/j2ee/cluster/server0/./temp/classpath_resolver/Mape05ccfb0860c11db9bb7000d60de069c/source/com/sap/xi/tf/_MM_Source_Target_.java:47: int cannot be dereferenced

return result.toString();

^

Former Member
0 Kudos

I thought something like this would work but it doesn't....

int one;

int two;

int result = one+two;

if (a.equals(b))

one = 20;

two = 30;

else

one = 50;

two = 50;

return result.toString();

This is the error message I get

Source code has syntax error:

/usr/sap/CXD/DVEBMGS00/j2ee/cluster/server0/./temp/classpath_resolver/Map27f663f0860c11db96de000d60de069c/source/com/sap/xi/tf/_MM_Source_Target_.java:42: 'else' without 'if'

else

^

1 error

Message was edited by:

Alex Ong

MichalKrawczyk
Active Contributor
0 Kudos

hi,

try this:

int one;

int two;

if (a.equals(b)) {

one = 20;

two = 30;

}

else{

one = 50;

two = 50;

}

int result = one+two;

return result.toString();

Regards,

michal

Former Member
0 Kudos

try,



int one;
int two;

if (a.equals(b)){
one = 20;
two = 30;
}
else{
one = 50;
two = 50;
}
int result = one+two;
return result.toString();

Regards,

Robin

MichalKrawczyk
Active Contributor
0 Kudos

BTW

I didn't test my code but it should work ... I hope:)

Regards,

michal

Former Member
0 Kudos

Only chicken test there code before posting

Regards,

Robin

Former Member
0 Kudos

OK fair enough... What is the difference between what they return?

Also can someone show a basic UDF where you input 2 strings... af they match then you have 2 integers which are equal to 20 and 30.

If the strings do not match then the 2 integers take on different values i.e. 50 and 100.

The result is the 2 integers added together...

Former Member
0 Kudos

Is this not possible in a simple UDF? To use Integers is it mandatory to user advanced UDF's?

Former Member
0 Kudos

Hi ,

You can use integer in simple UDF also.

Thanks,

Tuhin

MichalKrawczyk
Active Contributor
0 Kudos

hi,

not it's not you can use a simple one

BTW

simple one and advanced are the same (it's just what do they return)

Regards,

michal

Former Member
0 Kudos

Hi Alex,

This is the sample code

public void function(String[] value,ResultList result,Container container){

double sum=0;

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

double x=Double.parseDouble(value<i>);

sum=sum+x;

}

result.addValue(""+sum);

}

Regards.

Sai

bhavesh_kantilal
Active Contributor
0 Kudos

Alex,

Yes, all inputs and outputs from the UDF's are always Strings.

Inside the UDF, you would need to convert the Strings into Integers / Numbers using methofs like (Integer.parseInt() ) etc. , do the maniuplations needed and then convert the number to String using .toString()

Even if the output is String, and the element to which it is assigned is an Integrer, it does not matter as the validation against the datatype is not done at the mapping and as long as it is a number, the target system will also not have an issue.

The point of ensuring and checking whther the data is a number or not lies with the UDF coder if needed or with the application system as per project requirements.

><i>Basically I am comfortable doing comparisons of strings but I would like to do scennarios which include integers and calculations.... or is this not possible in XI UDF's?</i>

Just convert the String to Integrer/ Number and do the comparison inside the UDF .

Regards,

Bhavesh

Former Member
0 Kudos

thank you guys... could you please let me know how to do this... perhaps give me some sample code?

former_member184619
Active Contributor
0 Kudos

hi,

u can use integer within your UDF and the convert it into string back.

The reason the o/p is string is that at last it is going to be converted as XML

that is STRING within the tags.

Reagrds

Sachin

stefan_grube
Active Contributor
0 Kudos

Hi Alex,

as inside an XML only strings are allowed and every decimal, date, currency is a string, the same is in input and output parameters of UDF.

But there is no problem to transform the string to any other type like BigDecimal or double to do calculations. The result of the calculations hat to be transformed back to strings.

Regards

Stefan

MichalKrawczyk
Active Contributor
0 Kudos

hi,

you can change the string to any int or any numeric

inside you UDF and work on that

and then change it back (the result)

Regards,

michal