-2

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();
          }

       }
    });
AndroidCB
  • 230
  • 4
  • 11

5 Answers5

3

Read about How do I compare strings in Java?


So Simply change

if((Global.controlNum == "1")||(Global.controlNum == "2" ))

With

if(("1".equals(Global.controlNum))||("2".equals(Global.controlNum) ))
Community
  • 1
  • 1
Pankaj Kumar
  • 81,967
  • 29
  • 167
  • 186
0

You should use equlas() to compare String

 Global.controlNum.equlas("1")
Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
0

Try replacing

if((Global.controlNum == "1")||(Global.controlNum == "2" ))

with

if((Global.controlNum.equalsIgnoreCase("1"))||(Global.controlNum.equalsIgnoreCase("2") ))
Anuj
  • 389
  • 1
  • 5
  • 20
0

Use Global.controlNum.equals("1")

If you want to compare Strings in Java (Android) always use the method equals(String) or equalsIgnoreCase(String)

dumazy
  • 13,857
  • 12
  • 66
  • 113
0

you should change it with:

if((Global.controlNum == "1")||(Global.controlNum == "2" ))

to

if((Global.controlNum.equals("1"))||(Global.controlNum.equals("2") ))
Piyush
  • 18,895
  • 5
  • 32
  • 63
  • @AndroidLearner I really doubt that. Unless the `toString` method that creates it can return null, which seems unlikely, then it's fine. – resueman Aug 21 '13 at 12:54