Reputation: 25
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?
Upvotes: 1
Views: 47
Reputation: 2143
In Java
, for comparisson objects of non-primitive
data type you must use equals() method.
Upvotes: 0
Reputation: 312
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
Upvotes: 0
Reputation: 38098
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))
Upvotes: 2