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
  • 724
  • 2
  • 8
  • 26
  • Compare your strings using `equals` (see *[How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java)*). If the issue is still there look at the values of both variables. – Howard May 19 '12 at 17:06
  • try single stepping through it, e.g. in Firebug. – Julian May 19 '12 at 17:09
  • Try if string.compareTo(other_string)==0 – scrineym May 19 '12 at 17:10
  • I solved it. The mistake is that I used "<=Array.length()" instead of "<". Thank you all anyway. – stanga bogdan May 19 '12 at 17:14

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
  • 34,448
  • 50
  • 182
  • 322
BlackHatSamurai
  • 23,275
  • 22
  • 95
  • 156
  • I solved it. The mistake is that I used "<=Array.length()" instead of "<". Thank you all anyway. PS: It works in java to compare two strings using == . – stanga bogdan May 19 '12 at 22:51