#Guidance on OOP open ended problem please, thank you!

68 messages · Page 1 of 1 (latest)

waxen grove
#

im stumped... mainly because i havent seen non csv files. that being said. should i create a split("\n\n") method and then a split("\n")?

file.txt

Pancake dough
60
milk
egg
flour
salt
butter

Meatballs
20
ground meat
egg
breadcrumbs

Tofu rolls
30
tofu
rice
water
carrot
cucumber
avocado
wasabi
import java.util.ArrayList;
import java.util.Scanner;

public class RecipeSearch {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        ArrayList<String> recipes = new ArrayList<>();
        
        System.out.println("File to read: ");
        String file = scanner.nextLine();
        
        UserInterface ui = new UserInterface(file);
        ui.start();

        System.out.println("Commands:");
        System.out.println("list - lists the reciples");
        System.out.println("stop - stops the program");
//        System.out.println("find name - searches recipes by name");

        ui.command();

    }

}
plain graniteBOT
#

This post has been reserved for your question.

Hey @waxen grove! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

waxen grove
#
import java.nio.file.Paths;

import java.util.Scanner;

public class UserInterface {

    private String file;
    private Recipes recipes;
    private Scanner scanner;

    public UserInterface(String files) {
        this.recipes = recipes;
        this.scanner = scanner;
    }

    public void start() {
        try ( Scanner read = new Scanner(Paths.get(file))) {
            while (read.hasNextLine()) {
                String line = read.nextLine();
                if (line.isEmpty()) {

                    String[] food = line.split("\n");
                    String name = food[0];
                    int time = Integer.valueOf(food[1]);
                    String i1 = food[2];
                    String i2 = food[3];
                    String i3 = food[4];
                    String i4 = food[5];
                    String i5 = food[6];
                    String i6 = food[7];
                    String i7 = food[8];
                    recipe.add(new String(name, time,))
                }
            }
        } catch (Exception e) {
            System.out.println("Error: " + e.getMessage());
        }
    }

    public void command() {
        System.out.print("Enter command:");
        String command = scanner.nextLine();

        if (command.equals("stop")) {
            break;
        }
        if (command.equals("list")) {
            System.out.println("Recipes");
            for (String nameAndTime : recipes) {
                System.out.println(nameAndTime.get(0) + ", cooking time: " + nameAndTime.get(1));

            }
        }
        if (command.equals("find name")) {

        }
    }
}
#

the other thing stumping me, there isnt a set amount of ingredients, so i just created a bunch of spaces in case but i dont know if thatll affect the recipes with less than 7 ingredients

#
public class Recipes {
    private String name;
    private String time;

    public Recipes() {
    this.name = name;
    this.time = time;
    
    }
    
//  Integer.valueOf(this.time);
    
}
humble mantle
humble mantle
waxen grove
humble mantle
#

you generally don't...

#

choose 1, array or list

#

an ArrayList is a list that uses an array, it is not an array

#

anyways the dynamic amount just isn't an issue, you just create a array with a dynamic size

#

String.split already gives you a number on how many elements are present

waxen grove
humble mantle
#

oh you're doing nextLine on the file

#

if you're doing that there's not going to be any \n to split by at all

#

if you want to split, read out the entire file

#

you would then split on \n\n first

#

with a csv, you would be splitting on \n (via nextLine) to separate each listing, then split on , (via split) to get the elements of each listing

waxen grove
#

not a csv

humble mantle
#

i know it isn't

waxen grove
#

gotcha

humble mantle
#

you said you've done csvs so im using that as a reference point

#

although technically the file you showed is a valid csv...

waxen grove
#

dont understand how? unless i edit the file (which i cant)

humble mantle
#

??????

#

i haven't told you to do anything at this point

waxen grove
#

so if i do a string.split("\n\n"), how do i iterate thru the different recipes? wont string.split("\n\n") create 3 blocks that have one idex?
thats why i wanted to create an arraylist from the arrays

humble mantle
#

that have one index
waitWhat

#

what is this supposed to mean

waxen grove
waxen grove
humble mantle
humble mantle
#

im not seeing what you think is the issue there

waxen grove
#

that i need to then iterate thru the lines to pull info

humble mantle
#

sure

waxen grove
#

meaning?

humble mantle
#

....you need to iterate through the lines to pull info

#

exactly as you said

#

what are you confused about?

waxen grove
#

this is the fuctionality i need

File to read: recipes.txt

Commands:
list - lists the recipes
stop - stops the program

Enter command: list

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

Enter command:  stop
humble mantle
#

that doesn't tell me anything lmao

#

and it's not info i need

waxen grove
#

gimme a sec

#

im typing

humble mantle
#

no, i won't

#

i don't need whatever you're typing

waxen grove
#

cool, then i dont need your help

humble mantle
#

right now you haven't made clear what you think is the issue here

#

i can't help you if you don't tell me what to help with. giving context here is.. nice, but it's not what i need to help you

waxen grove
#

you ask me for something, i try to make it clear, and you barrage me

humble mantle
#

because your attempt to make it clear doesn't answer my question...

waxen grove
#

bro.. literally going in circlees. i asked if i could do something and you told me not to because its dumb. now im basically asking the same thing cause we came back to the same issue

#

and youre gonna call me dumb again

humble mantle
#

when using this txt, or a csv, or any other kind of format similar to this, you need to split twice. once to separate entries, and again to separate data.
that split is not necessarily via split, you may have had nextLine on scanner as the first split when using csv as each row/line would be a single entry

here, nextLine will not work for separating entries. if you want to use the scanner, you can set the delimiter to \n\n and use next, which will use that delimiter to split, or you can read out the entire file to use String.split. then you can use \n as a delimiter via nextLine, next, or split the whole chunk via String.split

humble mantle
waxen grove
waxen grove
humble mantle
humble mantle
waxen grove
humble mantle
#

lmao that's not belittling

waxen grove
#

k

#

thanks for your help and time

plain graniteBOT
# waxen grove thanks for your help and time

If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.