0

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?

Picture of Error

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;

}

}

DaveyDaveDave
  • 9,821
  • 11
  • 64
  • 77

2 Answers2

0
public void NutritionInfo(float totFat, float satFat, float transFat,
        float totCarbs, float dietaryFib, float totSugars, float protein )

NutritionInfo takes in 7 parameters- therefore, you can not simply call it without any as you do in your case. You have to call the method and supply it with parameters. If you want to simply run the method without any parameters and initialize those variables using the Scanner input (as it appears in your code), make them local variables instead, e.g. something like:

public void NutritionInfo() {
float totFat, satFat, transFat, totCarbs, dietaryFib, totSugars, protein;

Since it is a non-static method, it would be accessed like this:

Recipe recipe = new Recipe();
recipe.NutritionInfo();

That would be even better, because it is not good practice to give so many arguments to a method - the rule of thumb (does not apply always though) is that the less arguments, the better ideally.

vs97
  • 5,765
  • 3
  • 28
  • 41
  • When I removed the parameters from the method, on the last line where the object called "newRecipe", the variables are green colored and I get an error saying that a non-static variable cannot be referenced from static context. Could this be because I need to put "this.totFat" instead? – WeezleXX89 Feb 24 '19 at 01:43
0

Your NutritionInfo method asks for seven inputs. When you call it, you need to have:

NutritionInfo([input 1], [input 2], etc..., [input 7]);

or you can make a new function requiring no inputs.

Jack Papel
  • 51
  • 2
  • 3