i am doing one of the last ex of the java-programming.mooc.fi PART 7 Recipe search (4 parts). Im almost finished but something its wrong when I initiate the instances for the object Recetas. For some reason each instance is OK but "ingredientes" its overwritten with the new data in all the instances. pls find below the code and thanks in advance for your help!!
The problem its here:
public ArrayList<ReceiptAdmin> FileToRecetas(){
ArrayList <String> ingredientes = new ArrayList<>();
int i = 1;
String nombreReceta = "";
int tiempo = 0;
for (String data : fileToArray){
if (data.equals("")){
//System.out.println(ingredientes);
recetas.add(new ReceiptAdmin(nombreReceta, tiempo, ingredientes));
//System.out.println(recetas);
i = 1;
ingredientes.clear();
continue;
}
if (i==1){
nombreReceta = data;
}
if (i==2){
tiempo = Integer.valueOf(data);
}
if (i > 2){
ingredientes.add(data);
}
i++;
}
//System.out.println(ingredientes);
//System.out.println(ingredientes);
this.recetas.add(new ReceiptAdmin(nombreReceta, tiempo, ingredientes));
//System.out.println(recetas);
return recetas;
}
the output for testing is :
Pancake dough, cooking time: 60[tofu, rice, water, carrot, cucumber, avocado, wasabi],
Meatballs, cooking time: 20[tofu, rice, water, carrot, cucumber, avocado, wasabi],
Tofu rolls, cooking time: 30[tofu, rice, water, carrot, cucumber, avocado, wasabi]
All tofu ingredients!!
UserInterface.java https://pastebin.com/twvXv04j
recipes.txt https://pastebin.com/Nwz1RJa7
RecipeAdmin.java https://pastebin.com/1TEYssgS
RecipeSearch.java https://pastebin.com/4MbsGeYz