(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.