inrob
inrob

Reputation: 5089

java update string value with if else

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?

Upvotes: 0

Views: 700

Answers (5)

danijoo
danijoo

Reputation: 2843

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";
}

Upvotes: 0

Gopi
Gopi

Reputation: 712

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

Upvotes: 0

richie
richie

Reputation: 199

== 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.

Upvotes: 0

Kirti Thorat
Kirti Thorat

Reputation: 53018

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";
    }

Upvotes: 0

Khaelex
Khaelex

Reputation: 762

== 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.

Upvotes: 6

Related Questions