Wednesday, 21 August 2013

Simple if statement stops code from working [duplicate]

Simple if statement stops code from working [duplicate]

This question already has an answer here:
How do I compare strings in Java? 28 answers
I'm having a problem with a simple if statement and i'm not too sure why.
I've got an edittext box and a button, when the user inputs a value into
the edittext and then presses the button, whatever was input into the box
is converted to string and stored in a variable, this variable is then
displayed in a toast. Now this works perfectly fine as it is but I would
like it to only display if a certain value is input into the editbox but
when I put in an if statement to validate this, it seems to completely
disgregard the if statement and does nothing. It does not cause any errors
but it stops any toast from being displayed even if the correct string is
input. I'm sure this is something simple but I can't seem to work it out.
It would be great if anyone could work out why it does this.
Code below:

Working code when the if statement is commented out:

saveBtn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Global.controlNum = inputTxt1.getText().toString();
// if((Global.controlNum == "1")||(Global.controlNum == "2" )){
Toast toast= Toast.makeText(SettingsScreen.this,"hello " +
Global.controlNum, Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, -100);
toast.show();
// }
}
});

if the if statement is brought in then it will do nothing:

saveBtn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Global.controlNum = inputTxt1.getText().toString();
if((Global.controlNum == "1")||(Global.controlNum == "2" )){
Toast toast= Toast.makeText(SettingsScreen.this,"hello "
+ Global.controlNum, Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, -100);
toast.show();
}
}
});

No comments:

Post a Comment