0

The problem is that I expect the text that I read from a webpage calling grabURL is absolutely equal to what I put into String lorem in the onPostExecute.

But Android says: "No, it does not!"

Why do I think Android is not right about equality?

1) StackTrace screenshot:

STACKTRACE

2) Screenphoto:

NOT EQUAL

3) You can find String lorem = "Lorem ipsum dolor sit amet"; downward in my class, and look through the source code of the webpage I'm reading text from:

SOURCE CODE

Source code of my classes:

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        grabURL("http://www.cyberweb.pp.ua/x/o.html");

    }

    public void grabURL(String url) {
        new GrabURL().execute(url);
    }

    private class GrabURL extends AsyncTask<String, Void, Void> {
        private final HttpClient Client = new DefaultHttpClient();
        private String Content;
        private String Error = null;

        protected Void doInBackground(String... urls) {
            try {

                HttpGet httpget = new HttpGet(urls[0]);
                ResponseHandler<String> responseHandler = new BasicResponseHandler();
                Content = Client.execute(httpget, responseHandler);

            } catch (ClientProtocolException e) {
                Error = e.getMessage();
                cancel(true);
            } catch (IOException e) {
                Error = e.getMessage();
                cancel(true);
            }

            return null;
        }

        protected void onPostExecute(Void unused) {

            String lorem = "Lorem ipsum dolor sit amet";

            if (Error != null) {
                Toast.makeText(MainActivity.this, Error, Toast.LENGTH_LONG)
                        .show();
            } else {

                TextView xyu = (TextView) findViewById(R.id.xyu);
                TextView xyu1 = (TextView) findViewById(R.id.xyu1);
                TextView xyu2 = (TextView) findViewById(R.id.xyu2);

                xyu.setText(Content);
                xyu1.setText(lorem);

                Log.v("lorem", lorem);
                Log.v("Content", Content);

                String eq = null;


                if (Content.length() != lorem.length()) {
                       Log.v("length?", "Different length");
                    }


                if (Content.trim().equalsIgnoreCase(lorem.trim())) {

                    xyu2.setText("EQUAL");
                    eq = "EQUAL";
                    Log.v("equal?", eq);
                }

                else {

                    xyu2.setText("NOT EQUAL");
                    eq = "NOT EQUAL";
                    Log.v("equal?", eq);
                }

            }
        }

    }
}

This issue is driving me nuts.

Сan anybody guess what happens? Thank you in advance!

Last Stacktrace:

enter image description here

The issue was caused by webpage encoding. Equality is correct when changing the encoding to UTF-8.

Ivan Fazaniuk
  • 1,062
  • 3
  • 11
  • 26
  • possible duplicate of [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Michael Petrotta Jan 06 '13 at 22:33
  • 2
    One thing we can say for sure is that Java's String.equals() method works. The only nonsense can be in your code... – Simon Jan 06 '13 at 22:41

3 Answers3

3

You cannot use == to compare Strings in Java. Use equals().

The answer here describes why in great detail: How do I compare strings in Java?

Community
  • 1
  • 1
Sam
  • 86,580
  • 20
  • 181
  • 179
  • Thanks but that's not a reason, I've updated code and it's still "NOT EQUAL" – Ivan Fazaniuk Jan 06 '13 at 22:35
  • It's a big reason, but apparently there is more... Maybe there is white space on the end of the URL line, try: `Content.trim().equals(lorem)`. – Sam Jan 06 '13 at 22:38
3

Try also comparing the length:

final int cl = Content.length();
final int ll = lorem.length();
if (cl != ll) {
   Log.v("TAG", "Different length (Content=" + cl + ", lorem=" + ll + ")");
}
Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134
  • @IvanFazaniuk What about `(Content.trim().length() != lorem.length())`? – Sam Jan 06 '13 at 22:48
  • @Sam "Different length" and "NOT EQUAL". Can this problem be related to the webpage encoding? – Ivan Fazaniuk Jan 06 '13 at 22:56
  • @dtmilano Content has one character more than lorem. Always! even if I left only one letter "L" on the webpage and in the lorem string. – Ivan Fazaniuk Jan 06 '13 at 23:07
  • 2
    @IvanFazaniuk I can only assume that there is some type of whitespace character that `trim()` is not removing. But I have no idea what it is... This might help: [Why trim is not working?](http://stackoverflow.com/a/4728647/1267661). If it is only one trailing character you can use `Content = Content.substring(0, content.length() - 1);` (But this assumes that character will always be there...) – Sam Jan 06 '13 at 23:10
  • Thanks you all for suggestion of a direction for thoughts, I'll be looking for that "secret" symbol now :) – Ivan Fazaniuk Jan 06 '13 at 23:15
2

Don't use == for String comparisons.

Use .equals() instead, it will actually check the content of each String and return the proper result.

if (Content.equals(lorem)) {
    //etc...

Edit: If you're still not getting an equality, you can try this:

if (Content.trim().equalsIgnoreCase(lorem.trim())) {
        //etc...

This trims all whitespace and performs the check regardless of case. It's the most you can do to check two Strings.

A--C
  • 36,351
  • 10
  • 106
  • 92