0

I'm new to java and I'm encountering a problem that doesn't make sense for me. I have a list view that launches an activity and the activity has to read some intent values in order to display the correct information.

    // getting intent data
    Intent in = getIntent();
    // Get name
    final String catName = in.getStringExtra("category").toString();
    //Textview
    TextView categorytw= (TextView)findViewById(R.id.category_name);
    categorytw.setText(catName);

    // Category URL
    String catUrl = "";

    if( catName =="Italy" ){
        catUrl = "http://url .com/italy.php";
    }
    else if( catName =="Belgium"){
        catUrl = "http://url .com/italy.php";
    }
    else if( catName =="France"){
        catUrl = "http://url .com/france.php";
    }

As You can see I get the intent value "category" and assign it to catName variable, and then I update the title textview categorytw

So far everything is good, categorytw is updated according to the value of catName.

Now I need to fetch a particular url based on the name of the category, that is why i initialize catUrl, which is empty at the beginning. Based on the value of catName, catUrl will have a different value.

This value/url is later used when I fetch some data from the url (catUrl) :

try{
XMLParser parser = new XMLParser();
String xml   = parser.getXmlFromUrl(catUrl );
}

the problem is that catUrl is always empty. I double checked catName values.

Does anybody know what might be wrong here?

3
  • By the way, you have %20 symbol after //url. (encoded) Commented Mar 12, 2014 at 15:06
  • possible duplicate of How do I compare strings in Java? Commented Mar 12, 2014 at 15:14
  • You should accept an answer to "close" you question. Commented Mar 12, 2014 at 19:14

5 Answers 5

6

== in Java compares Strings by reference (i.e. are the variables pointing to the same location in memory), not by value. Use catName.equals("Italy") to compare by value.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks Khaelid. I never would have thought that. I have used == in another project and it worked properly... Strange.
@bornie it works for integers and other primitive data types, but for strings and objects it won't compare by value. In some cases, using == to compare strings will work. For example, if you did String a = "cat"; String b = a; if (b==a) it would work, because they are the same location in memory.
@bornie look for String pool. That's probably the case where you compared successfully strings with ==
0

Use .equals to compare strings and not ==.

== compares reference equality.

.equals compares value equality.

In your case, as "Italy", "Belgium", "France" and catName are pointing to different objects. None of your conditions would ever match. Hence, catUrl is always empty.

Update your code as below:

   if( "Italy".equals(catName)){
        catUrl = "http://url.com/italy.php"; ## Remove the space between url and .com
    }
    else if("Belgium".equals(catName)){
        catUrl = "http://url.com/italy.php";
    }
    else if( "France".equals(catName)){
        catUrl = "http://url.com/france.php";
    }

2 Comments

To elaborate on that, == checks if the references on either side are pointing to the same object. .equals() is your implementation to see whether two different objects are "equal".
@slider Added that to the answer. :)
0

== in Java programming language means that it will compare memory address with the other object. In your snippet, when you call toString(), it will generate a new String object from heap as return. The logic in if-else statment it uses a String that from Constant pool differs with in heap object. You should use equals() for comparing string values, see JDK source code for details.

Comments

0

Declare catUrl as StringBuilder, use StringBuilder.appen() to add new values to it.

Comments

0

Strings cant be compared like primitive variables in java. The reason is that == only compares the references. So if you check if (1 == 1), it is evaluated as true as the jvm reuses primitives instead of allocating new memory for it.

String work different. In Java, strings are no primitive variables which is why every time you write "this is a string", a new object is generated and space is allocated in memory, although its the same text in the quotes. You can think of that as if you would do something like new String(// all the characters of a string) if you want. This is qhy you have to use "some string".equals("another string") to compare string. The check with equals does not only compare the reference. It checks if the object has the same value, which is why this will work.

So the correct way would be:

if( catName.equals("Italy") ){
    catUrl = "http://url .com/italy.php";
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.