I have a recipe class which has a constructor, the constructor takes in 7 parameters =
RecipeName,AuthorName,Ingredients,Ingredientamounts,rating,servingSize,Instructions

I'm trying to set those values using the scanner, but I don't know how to make it work since I have the scanner method inside of the Recipe class.

So it looks kind of like this in pseudocode .
Please take into count, that I will have a an array of this class because there will be multiple recipes the user will select with a JFileChooser. There will also be a JFrame but that will be easier.

public class Recipe
{
String nameOfFileandDirectory = "User sets value";
File file = new File( nameOfFileandDirectory);
Scanner scanner = new Scanner(file);

String scannerLine = scanner.nextLine();
String int double etc; // these are all of the variables the scanner will get
public Recipe(etc)
{
etc = etc; // These will be obviously more variables, but for simplicity am only getting one;
}
}

PM'ed you :).

Why are you using the Scanner class inside of the Recipe class if you want to gather the values to be used in the Recipe class's constructor? Should you get the values in another class and then call the Recipe class's constructor with those values.

Why are you using the Scanner class inside of the Recipe class if you want to gather the values to be used in the Recipe class's constructor? Should you get the values in another class and then call the Recipe class's constructor with those values.

Is it ok inside of the main class?
I just feel afraid to place it in the main class since there will be different recipes.
can a method array?

Its up to you where you get the data the class needs.
Do you want to create a class with an empty constructor and then have the class ask the user for the data it needs.
Or do you want to get all the data that the class needs in another class and pass the data to the class in the class's constructor.

Its up to you where you get the data the class needs.
Do you want to create a class with an empty constructor and then have the class ask the user for the data it needs.
Or do you want to get all the data that the class needs in another class and pass the data to the class in the class's constructor.

Well the user will hopefully add a textfile with a JFileChooser which I haven't implemented just yet, and therefore I believe it will ask the user thought I really can't visualize how to make new arrays of a scanner when the user wants to look at a different file.

What people usually do is something like this:
2 classes, Recipe - one recipe per instance, and Cookbook - one instance of Cookbook has lots of Recipes (eg an array of Recipes). Cookbook has the method that opens the Scanner, reads in the user's data, creates instances of Recipe (by calling Recipe's constructor) and adds each Recipe to Cookbook's array of Recipes.

What people usually do is something like this:
2 classes, Recipe - one recipe per instance, and Cookbook - one instance of Cookbook has lots of Recipes (eg an array of Recipes). Cookbook has the method that opens the Scanner, reads in the user's data, creates instances of Recipe (by calling Recipe's constructor) and adds each Recipe to Cookbook's array of Recipes.

Thanks tihs was helpful. I hope you don't mind me naming my class cookbook :D.

By the way I came upon a little problem while doing this, when in the recipe constructor I have the ingredientNames[] variable at the top.

Now there will be number of ingredients for different recipes, therefore I can't visualize how to get all of the ingredientNames from the scanner and place them in the constructor, wow I can't even explain it.. let me try that in code.

public class Recipe
{
private String recipeNames[]; //  Lets say the recipe has 2 ingredients, how will I tell the program to only place 2 ingredients here?
//and the rest is whitespace

      public Recipe(String[] recipeNames(there can be all the way from 1- 40 recipes over here))
{

}

}

sorry for the bad explanation, is just hard for me to visualize this.

That looks like a reasonable approach. Gather all the ingredients into a container (like an array or ArrayList) and pass that to the constructor.

Your code shows you are passing an array of recipenames to each recipe? Did you mean that to be the array of ingredients?

That looks like a reasonable approach. Gather all the ingredients into a container (like an array or ArrayList) and pass that to the constructor.

Your code shows you are passing an array of recipenames to each recipe? Did you mean that to be the array of ingredients?

Yes sorry for that, I was trying to type "Quickly".

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.