0

(Apologies for the formatting this is my first post)

I am looking at teaching myself Java through mooc.fi Java from the university of Helsinki, I have been attempting this problem for 4 days straight and just cant seam to get past it.The problem requires me to read recipes from a list (Recipe includes name , cooking time , Ingredients all on separate lines) with recipes separated by an empty line, the textfile is as follows.

Pancake dough 
60 
milk
egg
flour 
sugar 
salt 
butter

Meatballs 
20 
ground 
meat 
egg 
breadcrumbs

Tofu rolls 
30 
tofu rice 
water 
carrot 
cucumber 
avocado 
wasabi

It saves the correct amount of recipes but they all have the same output. My output is as follows

Pancake dough, cooking time: 60 
Pancake dough, cooking time: 60 
Pancake dough, cooking time: 60 

The output should be.

Pancake dough, cooking time: 60 
Meatballs, cooking time: 20 
Tofu rolls, cooking time: 30 

My main method.

public UserInterface(){

}

public void start(){

 ArrayList<Recipe> book = new ArrayList<>();

 String row="" ;

 try (Scanner scanner = new Scanner(Paths.get("recipes.txt"))){
     ArrayList<String> item = new ArrayList<>();

     
     while (scanner.hasNextLine()){

     
     while (scanner.hasNextLine()) {
         row = scanner.nextLine();
         if (row.equals("")) {
             break;
             
         }else{
             item.add(row);

         }

     }
         book.add(new Recipe(item));

     }
     
 } catch (Exception e) {
 }
 
 for (int i = 0; i < book.size(); i++) {
     System.out.println(book.get(i).list());
     
 }
}

}

Recipe Class.

import java.util.ArrayList;

public class Recipe {

private String name;
private int cookingTime;
private ArrayList<String> recipe;

public Recipe(ArrayList<String> r){
    this.recipe = r;



}

public String list(){
    return(this.recipeName()+", cooking time: "+this.cookingTime());
}

public String recipeName(){
    return this.recipe.get(0);
}

public int cookingTime(){
    return Integer.valueOf(this.recipe.get(1));

}
}

Your assistance will be appreciated

I attempted to read the sheet line for line with a scanner, the rows where saved into an array list Once the scanner detects a blank space it should save the arrayList to a recipe object. then continue from the next line and repeat.

0

1 Answer 1

0

The instantiation of the items List is in the wrong place at (1). And so this list just gets longer and longer and the first recipe remains at the top. Notice how the output shows the first recipe repeated many times.

You need to create a new items List each time you have a new Recipe... i.e. at (2).

        try (Scanner scanner = new Scanner(Paths.get("recipes.txt"))) {
            // (1) Wrong here: ArrayList<String> item = new ArrayList<>();

            while (scanner.hasNextLine()) {
                // (2) should be here:
                ArrayList<String> item = new ArrayList<>();

                while (scanner.hasNextLine()) {
                    row = scanner.nextLine();
                    if (row.equals("")) {
                        break;

                    } else {
                        item.add(row);

                    }
                }
                book.add(new Recipe(item));
                // (3)
            }
        }

You should try this other "solution" and see what happens: leave the code at (1) and call items.clear() after the Recipe is instantiated (3). What happens? and why?

1
  • Thank you so much you absolute legend , You where correct about where the instantiation of the list took place, Now I can finally have a good weekend and finish the last problem of the Mooc. Commented Mar 23 at 6:40

Not the answer you're looking for? Browse other questions tagged or ask your own question.