Discussion Closed This discussion was created more than 6 months ago and has been closed. To start a new discussion with a link back to this one, click here.

If-else not evaluating correctly in app builder method

Please login with a confirmed email address before reporting spam

I am using a combo box to plot different graphs depending on which option the user chooses from the combo box. To do this, I created a very simple method with some if-else statements. The given parameter for combo box methods is newValue, and it's a string. My if-else statements follow the pattern:

if (newValue == "varName1") {
// do something
} else if (newValue == "varName2") {
// do something else
} else if .....

For whatever reason, these statements are not evaluating correctly. To test that newValue has the value that I think it does, I put newValue a confirm dialogue box. For example, one of the options from my choice list is "Plot Electrical Potential" with a variable name of plotEP. The dialogue box pops up correctly, saying "plotEP" on it, but the if statement -- if (newValue == "plotEP") -- evaluates to false.

I realize this is a very simple issue but I don't know what could be going wrong. Thanks.

1 Reply Last Post 10.05.2016, 16:08 GMT-4
Edgar J. Kaiser Certified Consultant

Please login with a confirmed email address before reporting spam

Posted: 8 years ago 10.05.2016, 16:08 GMT-4
David,

in JAVA the "==" operator checks whether the operands are the same object not whether the strings are identical. In your case identical strings are in different objects.

You have to use the equals() method:
newValue.equals("VarName2")

Just like you it took me significant time to find that out. I was coming from C when I had to get familiar with JAVA to make my first applications.

Cheers
Edgar

--
Edgar J. Kaiser
emPhys Physical Technology
www.emphys.com
David, in JAVA the "==" operator checks whether the operands are the same object not whether the strings are identical. In your case identical strings are in different objects. You have to use the equals() method: newValue.equals("VarName2") Just like you it took me significant time to find that out. I was coming from C when I had to get familiar with JAVA to make my first applications. Cheers Edgar -- Edgar J. Kaiser emPhys Physical Technology http://www.emphys.com

Note that while COMSOL employees may participate in the discussion forum, COMSOL® software users who are on-subscription should submit their questions via the Support Center for a more comprehensive response from the Technical Support team.