1

I'm a BEGINNER in java. My project is a simple calorie counter.

The way it should work:

Enter calorie intake number: (user adds number)

program subtract users input by 2,000 calories

As of now, my program is subtracting the parameter for the food object from the 2,000 cal. I know this because I'VE DONE A LOT OF TESTING TRYING TO FIGURE OUT THE SOLUTION TO THIS PROBLEM MYSELF. If i had not changed the parameters for the cheesecake flavors methods to include a string and an integer, than it would have subtracted the calorie int from the last listed cake flavor(carmel) that held the variable calorie.

The problem:

I can seem to figure out how to connect the user input to the calorie calculation coded in the food class above the main. I've also tried changing the Scanner input variable to the calorie variable, but it then could not be resolved.

I'm listening my code now below. I'd be very grateful to anyone that could help me. Please remember, I'm a beginner, and read everything i've written before responding.

Thank you

    int calculateCaloriesLeft() {

        int caloriesLeft = 2000 - calories;
        return caloriesLeft;
    }



        Scanner input = new Scanner(System.in);
        System.out.println("Enter calorie of food: ");
        int value = 0;
        value = input.nextInt();

        value = deserts.calculateCaloriesLeft();
        System.out.println("calories left: " + value);


    }



}
quasar-light
  • 43
  • 1
  • 10

1 Answers1

1

You are not passing the entered value into the calculateCaloriesLeft method. You would need to add a parameter and pass the users input into it.

    Scanner input = new Scanner(System.in);
    System.out.println("Enter calorie of food: ");
    int value = 0;
    value = input.nextInt();

    value = deserts.calculateCaloriesLeft(value);
    System.out.println("calories left: " + value);

And your method would be

int calculateCaloriesLeft(int userCalories) {

    int caloriesLeft = 2000 - userCalories;
    return caloriesLeft;
}
gallen
  • 1,252
  • 1
  • 18
  • 25
  • Thank you so much! I've spent a week trying to figure out what was it the missing link! – quasar-light Feb 12 '17 at 17:08
  • No problem! I'm glad I could help. If this answer solved your issue, be sure to mark it as the accepted answer for future visitors. :) – gallen Feb 12 '17 at 17:14