0

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

2 Answers 2

3
ArrayList <String> ingredientes = new ArrayList<>();

needs to be in every loop. You can't just clear the list and start over, you have to make a new list.

The simplest fix is to replace ingredientes.clear() with ingredientes = new ArrayList<>();

Sign up to request clarification or add additional context in comments.

Comments

0

Problem is you are over writing the same ArrayList. To solve this issue you need to re initialize ingredients reference variable with a new ArrayList object every time.

Refactor the code like below.

if (data.equals("")){
         
            recetas.add(new ReceiptAdmin(nombreReceta, tiempo, ingredientes));
            i = 1;
            ArrayList <String> ingredientes = new ArrayList<>();//Change ingredients.clear() to re initializing a new list with new ArrayList<>().
            continue;

 }   

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.