-2

Why the if-else part isn't working in the code below. I checked the value of success just before if and it is returning true but the code inside if isn't working.

protected void onPostExecute(Void aVoid) {
              super.onPostExecute(aVoid);    
              dialog.dismiss();
              System.out.println(success);
              if(success == "true")
              {
                  Toast t = Toast.makeText(getApplicationContext(), "Successfully logged in", Toast.LENGTH_SHORT);
                    t.show();
                    Intent intent = new Intent(MainActivity.this, Quote_Details.class);         
                    intent.putExtra("sessionId", sessionId);
                    startActivity(intent);
              }
              else if(success == "fals")
              {
               Toast f = Toast.makeText(getApplicationContext(), "LOGIN FAILED", Toast.LENGTH_LONG);
               f.show();
              }
             }
Squonk
  • 48,331
  • 18
  • 101
  • 135
Amit Anand
  • 1,221
  • 1
  • 16
  • 40

4 Answers4

5

To compare strings, use ".equals".

E.g.:

if (success.equals("true"))

EDIT: oops, someone was faster... well, at least you have an example too now :)

Wildcopper
  • 373
  • 1
  • 3
  • 11
3

Since 'success' is a boolean variable(Or so it seems), you have to compare like you would compare all primitive values. So if(success){ } or if(success == true){ }. I would like to clarify the following - if(success) tells the if-statement to return value of success, if success is true, continue the statement, if false it returns false/doesn't continue the statement. It's a nicer way of doing it rather than if(success == true)

If success is a String then, if(success.equalsIgnoreCase("true")

This is because String is an object and not a primitive datatype like int, boolean, char etc.

You also compare objects the same way incase you required to do so in the future

Juxhin
  • 4,396
  • 7
  • 29
  • 53
2

You should compare strings with equals method, not with == operator. Operator == just compares the object references, not the content of the objects.

nogard
  • 9,067
  • 6
  • 31
  • 53
2

When you are dealing with String-objects use equals() to compare them:

protected void onPostExecute(Void aVoid) {
              super.onPostExecute(aVoid);    
              dialog.dismiss();
              System.out.println(success);
              if(success.equals("true"))
              {
                  Toast t = Toast.makeText(getApplicationContext(), "Successfully logged in", Toast.LENGTH_SHORT);
                    t.show();
                    Intent intent = new Intent(MainActivity.this, Quote_Details.class);         
                    intent.putExtra("sessionId", sessionId);
                    startActivity(intent);
              }
              else if(success.equals("fals"))
              {
               Toast f = Toast.makeText(getApplicationContext(), "LOGIN FAILED", Toast.LENGTH_LONG);
               f.show();
              }
             }
LionC
  • 3,088
  • 1
  • 23
  • 31