I have a method called NutritionInfo where the user enters the grams of total fat which uses variable totFat. This is displayed using the printRecipe method.
On the last line where the newRecipe object is created, I see a my totFat variable as green and I get an error saying:
non-static variable totFat cannot be referenced from a static context
How do I fix this line?
private String recipeName; // Stored value of recipe name
private int servings; // Amount of food per person for recipe
private float totFat = 0;
public void printRecipe() {
double singleServingCalories = totalRecipeCalories / servings;
System.out.println("Total Fat:" + getTotFat() + " in Grams.");
System.out.println("...Nutritional Information...");
System.out.println("Total Fat:" + totFat);
}
public void NutritionInfo() {
Scanner scnr = new Scanner(System.in);
System.out.println("Enter Total Fat: ");
while (!scnr.hasNextFloat()) {
System.out.println("Needs To Be An Float Type!");
System.out.println("Please Enter Total Fat In Grams");
scnr.next();
}
this.totFat = scnr.nextFloat();
}
public static Recipe createNewRecipe() {
Scanner scnr = new Scanner(System.in);
System.out.println("Please enter the recipe name: ");
String recipeName = scnr.nextLine();
System.out.println("How many servings: ");
while (!scnr.hasNextDouble()) {
int servings = scnr.nextInt();
System.out.println("Do you want to add any Nutritional Information? (y/n) ");
String response = scnr.next();
if (response.toLowerCase().equals("y")) {
Recipe recipe = new Recipe();
recipe.NutritionInfo();
}
Recipe newRecipe = new Recipe(recipeName, servings,totFat);
return newRecipe;
}
}
