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

       }
    });
6
  • controlNum is a string, it should be always compared using equals() or equalsIgnoreCase() methods. Aug 21, 2013 at 8:49
  • 1
    try Global.controlNum.equals("1") instead
    – rocketboy
    Aug 21, 2013 at 8:50
  • use .equals() or .equalIgonreCase() when u are comparing two strings.
    – user2618291
    Aug 21, 2013 at 8:50
  • No!! No!! Not == again..
    – Maroun
    Aug 21, 2013 at 8:52
  • 1
    @rocketboy better is "1".equals(Global.controlNum). Aug 21, 2013 at 8:53

5 Answers 5

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) ))
0

you should change it with:

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

to

if((Global.controlNum.equals("1"))||(Global.controlNum.equals("2") ))
1
  • @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, 2013 at 12:54
0

You should use equlas() to compare String

 Global.controlNum.equlas("1")
0

Try replacing

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

with

if((Global.controlNum.equalsIgnoreCase("1"))||(Global.controlNum.equalsIgnoreCase("2") ))
0

Use Global.controlNum.equals("1")

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.