#Part 7_07

1 messages · Page 1 of 1 (latest)

wise pendant
#

https://java-programming.mooc.fi/part-7

the second question can you gimme an idea how to solve it.

robust cobaltBOT
#

<@&987246399047479336> please have a look, thanks.

wise pendant
#

import java.io.File;
import java.util.ArrayList;
import java.util.Scanner;

public class RecipeSearch {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
                
        System.out.print("File to read: ");
        String fileName = scanner.nextLine();
        readRecipe readRecip = new readRecipe(fileName);//TRY AND CATCH SHI

        System.out.println("Commands:\nlist - lists the recipes\nstop - stops the program");
        String command = scanner.nextLine();

        while(true){
            if(command.equals("stop")){
                break;
            }
            if(command.equals("list")){
                System.out.println("Recepies");
                readRecip.listRecipes();
                break;
            }
        }
       
        
    }

}
#
import java.nio.file.Paths;
import java.util.Scanner;

public class readRecipe {//TRY AND CATCH CLASS
    Recipes recipe = new Recipes();

    public readRecipe(String s){
        

        try(Scanner scanner = new Scanner(Paths.get(s))) {
            
            while(scanner.hasNextLine()){
                String addition = scanner.nextLine();

                if(addition.equals("")){
                    recipe.addFullRecipe();
                } else {
                    recipe.add(addition);
                }
            }


        } catch (Exception e) {
            System.out.println("Error: " + e.getMessage());
        }
    }

    public void listRecipes(){
        recipe.getRecipes();
    }
}
#
import java.lang.reflect.Array;
import java.util.ArrayList;

public class Recipes {
    private ArrayList<ArrayList> listRecipes;
    private ArrayList<String> recipes;

    public Recipes(){
        this.recipes = new ArrayList<>();
        this.listRecipes = new ArrayList<>();
    }

    public void add(String s){
        this.recipes.add(s);
    }

    public void addFullRecipe(){
        this.listRecipes.add(recipes);
    }

    public void getRecipes(){
        for(String i: this.recipes){ 
            System.out.println(i);
        }
        for(ArrayList<ArrayList> i : this.listRecipes){
            System.out.println(i);
        }
    }
}
#

@sour violet

#

im trying and i haventused gpt yet

#

but ye

#

so i think we get the file as Name-Time-Ingredients-Blankspace-Repeat

#

so i thought why not make new arraylist every time it hits ""

sour violet
wise pendant
#

but my code has error

wise pendant
#

line

#

blank line

sour violet
wise pendant
#

im still thinking lol

#

recipe search is like the UI

#

readRecipe is like the try and catch method which uses Recipes class to store read recepies

#

recipes class has the arraylist of recipes

sour violet
#

btw you should rename String s to something more understandable, like String recipeFileLocation

wise pendant
#

i mean yea

#

but its like a one time read so i didnt mind

sour violet
#

ok, but which method is not working?

wise pendant
#

its not the method lemme tell ya

#

my idea was read the file-than when the new line comes add it to a list of arraylist idk how to say it

#

like here

#

but that clearly is dumb logic

#

you reckon i should start from start?

sour violet
#

wait i am thinking of how to explain this to you

wise pendant
#

sure

sour violet
#

wait your Recipes class is confusing me, is private ArrayList<String> recipes; supposed to be a single recipe?

#

@wise pendant

wise pendant
#

than add it to the arraylist<arraylist?

#

so taht every recipe has there own array list

#

and i can call arraylist[0} and [1] for name and time

sour violet
wise pendant
#

like what

#

oh

#

you want em to create a list of String arrays

sour violet
#

whenever you add a new recipe, you add a list or array to the recipe arraylist

wise pendant
#

ye

#

that is what i am trying

#

an arraylist with all recepies

#

the recepies are like

#

lemmme show

#

the list will contain this

#

yo

#

what if

#

we add it all until it hit the blank space

#

than add that to String[] then reset the whole arraylist?

#

what do you say

sour violet
#

yeah

#

sounds good

wise pendant
#

lemme try

#

want me to stream it?

sour violet
#

yes

sour violet
wise pendant
#

lemme tell ya