Dymanic if statement evaluation problem with string comparison

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Dymanic if statement evaluation problem with string comparison

Manikandan R-2
Hi Everyone,

I am able to do the string comparison using eval function with string
methods something like string.equals("test"), but not able to do something
like string == "test".

Here is the code which works fine - getting the success as output:

String s = "test";

String cond = "s.equals(\"test\")";

Interpreter i = new Interpreter();

i.set("s", s);
if((Boolean)i.eval(cond)) {
   System.out.println("success");
} else {
   System.out.println("fail");
}

Can anyone help me onhow to achieve the same using without string equals
method?
Thanks,
Mani
------------------------------------------------------------------------------

_______________________________________________
Beanshell-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/beanshell-users
Reply | Threaded
Open this post in threaded view
|

Re: Dymanic if statement evaluation problem with string comparison

Stewart Cambridge
What about using String.intern() ?

On 15 May 2010 04:47, Manikandan R <[hidden email]> wrote:

> Hi Everyone,
>
> I am able to do the string comparison using eval function with string
> methods something like string.equals("test"), but not able to do something
> like string == "test".
>
> Here is the code which works fine - getting the success as output:
>
> String s = "test";
>
> String cond = "s.equals(\"test\")";
>
> Interpreter i = new Interpreter();
>
> i.set("s", s);
> if((Boolean)i.eval(cond)) {
>   System.out.println("success");
> } else {
>   System.out.println("fail");
> }
>
> Can anyone help me onhow to achieve the same using without string equals
> method?
> Thanks,
> Mani
>
> ------------------------------------------------------------------------------
>
> _______________________________________________
> Beanshell-users mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/beanshell-users
>
------------------------------------------------------------------------------

_______________________________________________
Beanshell-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/beanshell-users
Reply | Threaded
Open this post in threaded view
|

Re: Dymanic if statement evaluation problem with string comparison

David Lee
In reply to this post by Manikandan R-2
the core issue here is that == doesnt do what you think it does, in bsh
or in java.
== compares 2 objects for identity , not the contents of 2 objects.
its possible for 2 objects to have the same value but not be the same object
Hence you should use equals()


http://www.xmlsh.org


On 5/14/2010 11:47 PM, Manikandan R wrote:

> Hi Everyone,
>
> I am able to do the string comparison using eval function with string
> methods something like string.equals("test"), but not able to do something
> like string == "test".
>
> Here is the code which works fine - getting the success as output:
>
> String s = "test";
>
> String cond = "s.equals(\"test\")";
>
> Interpreter i = new Interpreter();
>
> i.set("s", s);
> if((Boolean)i.eval(cond)) {
>     System.out.println("success");
> } else {
>     System.out.println("fail");
> }
>
> Can anyone help me onhow to achieve the same using without string equals
> method?
> Thanks,
> Mani
> ------------------------------------------------------------------------------
>
> _______________________________________________
> Beanshell-users mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/beanshell-users
>    

------------------------------------------------------------------------------

_______________________________________________
Beanshell-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/beanshell-users