We just started Java at school and I was wondering why this search algorithm isn't working: the purpose of the entire code is to store N wine bottles (object) in a cellar (array) and to search for the bottles with the same year and type (red, white, etc..) starting from an input given by the user. I used the same algorithm to search for wine bottles from a specific year and to return them as a StringBuilder and it worked.
public StringBuilder StampaTipo(String t) {
StringBuilder tipo= new StringBuilder(50);
int i=0;
for(i=0;i<this.cantina.length;i++) {
if(this.cantina[i].getTipo()==t) {
tipo.append(this.cantina[i].toString());
}
}
return tipo;
}
I was expecting to find the search results, this what i'm using in the main:
System.out.println("\nInserisci il tipo da cercare: ");
ricercaT=in.next();
System.out.println("\nBottiglie trovate: "+"\n"+c.StampaTipo(ricercaT));
"ricercaT" is the String input given by the user to find the bottles based on the type.
This is the output:
Insert the type of wine to find:
red
Bottles found: