cancel
Showing results for 
Search instead for 
Did you mean: 

String not accepted as input for UDF

Former Member
0 Kudos

Hi,

Strangest thing.

I write one simple UDF to determine on basis of input var1 if output should be var2 or var3; all var1,2 and 3 are strings.


public String determineAddress(String var1, String var2, String var3, Container container) throws StreamTransformationException{

String out = "";

if(var1=="1") {
out = var2;
}  else  {
out = var3; 
}
return out;

I have tested this UDF and strangely enough it always passes var3 as output. I even created same program in Eclipse to make sure i did it correctly and there it works good.

Then i did the following:

I changed var1 to type int. (and my code to: if(var1==1) etc)

This had the result that my udf produced the correct outcome.

Can anyone explain?

kr

Robert

Accepted Solutions (0)

Answers (1)

Answers (1)

Former Member
0 Kudos

try if (var1.equals("1"))

Former Member
0 Kudos

Hi Beena,

Yes that works. And it's a better solution than what i did.

I'm still wondering though why my original solution didnt work. To me that is a valid solution (as was proven in Eclipse)

Would it be a bug?

kr

Robert

Former Member
0 Kudos

Hi,

The compariosn operator == compares integer or objects but if you want to compare strings you must use .equals.

Regards,

Beena.

markangelo_dihiansan
Active Contributor
0 Kudos

Hello,

As was stated in this article:

http://abhisays.com/java/what-is-difference-between-and-equals-in-java.html

== compares the reference value of string object whereas equals() compares content of the string object.

If you still want to use the == sign, you need to do something similar to this


int temp = Integer.parseInt(var1);
if(temp == "1")

Hope this helps,

Mark

Edited by: Mark Dihiansan on Mar 30, 2011 12:26 PM

Former Member
0 Kudos

Hi Beena, Mark,

Hmmmm.....

It makes sense what you say, but as i said i tried same thing in Eclipse and it does give me the right result


public class Determine {
	
	public static void main(String [] args) {
		
		String out = "";
		String var1 = "1";
		String var2 = "X";
		String var3 = "Y";
		
		if (var1 == "1") {
			out = var2;
		}  else  {
			out = var3; 
		}
			System.out.println(out);
	}
}

This realyy gives me an X as output.

If you both are correct i cannot see why this works.

What is then the difference between what i create in UDF and here in JAVA class?

kr

Robert

Former Member
0 Kudos

The == operator checks to see if two objects are exactly the same object. Two strings may be different objects, but have the same value .

When you run this in xi its an argument to function and its different string object.

Try var1 = "1" in UDF before comparison , i think it will work.

Regards,

Beena.

Former Member
0 Kudos

Hi,

OK, i see the difference now. Not an easy one though.

The article was helpful.

Thx for your time.

Robert