Simple dummy question ... I'v created a method who send me a object after a for loop. The for loop make a return of my object in question. But Java ask me to send a return in the bottom of the method. The problem is... I don't have anything to return outside of my method. There is a ways to tell im to only return a error?
Here the code in question ... pretty simple!
public Stock getStockInfos(String selectedStock) {
for(Stock temp:registreStock){
if((temp.getSymbol()).equals(selectedStock)){
return temp;
}
}
}
Normally, I need to add a return statement at the end ... One solution I'v find, is to make a copy of temp and return this copy at the bottom. If this is the right solution, there is a easy way to copy/clone this object?
You must ask yourself if it could happen that you don't find a stock for the given symbol and what should happen in such a case. One option would be to return null or to raise an exception.
public Stock getStockInfos(String selectedStock) {
for(Stock temp:registreStock){
if((temp.getSymbol()).equals(selectedStock)){
return temp;
}
}
return null;
}