1

My searching part of code:

String neededShop = "not found";
float min_price=999999;
ProductHandler ph = new ProductHandler(this);
prod=ph.getAllProducts();
for(int i = 0 ; i<prod.size(); i++)
{
    if(prod.get(i).getName()==SearchProduct)
    {
        if(min_price>prod.get(i).getPrice())
        {   
            min_price=prod.get(i).getPrice();
            neededShop = prod.get(i).getShop();
        }
    }
}

in this part: if(prod.get(i).getName()==SearchProduct)

prod    [0] name    "Cat" (id=830032142800) 
SearchProduct   "Cat" (id=830032120832)

When it is comparing it returns false, in spite of they are similar. Any ideas why?

Makoto
  • 104,088
  • 27
  • 192
  • 230
newbie
  • 25
  • 5
  • possible duplicate of [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Makoto May 24 '15 at 03:19

3 Answers3

2

String comparison is not obtained by using the == operator.
You get the comparison by testing equals().

Try this:

if(prod.get(i).getName().equals(SearchProduct))
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
0

It isn't magic dude, its java... :) double equal == are used to primitive types only For comparing objects u need to check using equals() method

Kapil Vij
  • 312
  • 2
  • 7
0

In Java, for comparisson objects of non-primitive data type you must use equals() method.

Denys Vasylenko
  • 2,135
  • 18
  • 20