0

I have a for loop where I want to find an email.I have to strings which are for sure identical. In the for loop i put a toast and i saw that both show the same thing without any white spaces. What I have in if(String == String ) doesn't happen at all. Here is the code :

try {
        jArray = new JSONArray(result);
        JSONObject json_email = null;

        for(int i=0;i<=jArray.length();i++){
            json_email = jArray.getJSONObject(i);

            addedby2 = json_email.getString("email");



        if(addedby2==email2){
            Toast.makeText(this, json_email.toString(), Toast.LENGTH_LONG).show();
            user = json_email.getString("user");
            rpoints = json_email.getString("respectpoints");
            //Toast.makeText(this, user+"    "+rpoints, Toast.LENGTH_LONG).show();
        }
        }
        tvAdded.setText(user + " (" + rpoints + ")");
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

I also tried to getString("user") outside the if condition and it worked. I tried in if condition to write : String.equals(String) but without any result.

Does anyone know what the issue could be ?

stanga bogdan
  • 714
  • 2
  • 8
  • 25

1 Answers1

0

If you are using Java, to compare string values, you have to use the equals() function. Like Howard said, if that doesn't work, then your variables might be wrong.

BenMorel
  • 31,815
  • 47
  • 169
  • 296
BlackHatSamurai
  • 22,588
  • 21
  • 86
  • 152