I have here the following piece of code, it is designed to find and return a product object, using a string input to compare with names of all products.
public Product find(String input){
for(int i = 0; i <Products.size();i++)
{
if(input ==Products.get(i).getName()){
return Products.get(i);
}
}
}
I am having two problems with it.
A) The return value is inside the IF statement, so the main method find() does not have a return value. therefore I am receiving an error message about this.
B) If the user types in the name of a product that does not exist, then no product will be found, in which it shouldn't return anything, but I am not sure how to do this. The result is currently being used to remove products, so if it passes a null product object to be removed this would probably cause an error output in the remove method.
I am new to programming so my apologies if this is a novice question, any help would be very much appreciated.