Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialNatalia Henderson
5,190 PointsI need help with my JAVA project
The purpose of this program that I'm writing is to calculate a user's total daily energy expenditure. However, for some reason, it's giving me a ton of errors regarding my boolean operators and "if-else-if" statements. I feel like I'm missing something obvious, could you guys help? Also, I'm aware that not all of the code is on the format, for some reason, the treehouse pop-up won't let me put it all together, sorry!:
import java.util.Scanner;
public class TDEE
{
public static void main(String [] args)
{
double activityFactor;
double TDEE;
Scanner in = new Scanner(System.in);
System.out.print("Please enter your name: ");
String firstName = in.next();
String lastName = in.next();
System.out.println(firstName + " " + lastName);
System.out.print("Please enter your BMR: ");
double userBMR = in.nextDouble();
System.out.println(userBMR);
System.out.print("Please enter your gender (M/F): ");
String userGender = in.next();
System.out.println(userGender);
System.out.println();
System.out.println("Select your Activity Level");
System.out.println("[A] Resting (Sleeping, Reclining)");
System.out.println("[B] Sedentary (Minimal Movement)");
System.out.println("[C] Light (Sitting, Standing)");
System.out.println("[D] Moderate (Light Manual Labor, Dancing, Riding Bike)");
System.out.println("[E] Very Active (Team Sports, Hard Manual Labor)");
System.out.println("[F] Extremely Active (Full-Time Athlete, Heavy Manual Labor)");
System.out.println();
System.out.print("Enter the letter corresponding to your activity level: ");
String choice = in.next();
System.out.println(choice);
if(choice.equalsIgnoreCase("A"))
{
activityFactor = 1.0;
TDEE = userBMR * activityFactor;
}
else if (choice.equalsIgnoreCase("B"))
{
activityFactor = 1.3;
TDEE = userBMR * activityFactor;
}
}
else if (choice.equalsIgnoreCase("C")) && (userGender.equalsIgnoreCase("F"))
{
activityFactor = 1.5;
TDEE = userBMR * activityFactor;
}
else if ((choice.equalsIgnoreCase("C")) && (userGender.equalsIgnoreCase("M"))) || ((choice.equalsIgnoreCase("D")) && (userGender.equalsIgnoreCase("F")))
{
activityFactor = 1.6;
TDEE = userBMR * activityFactor;
}
else if (choice.equalsIgnoreCase("D")) && (userGender.equalsIgnoreCase("M"))
{
activityFactor = 1.7;
TDEE = userBMR * activityFactor;
}
else if (choice.equalsIgnoreCase("E")) && (userGender.equalsIgnoreCase("F"))
{
activityFactor = 1.9;
TDEE = userBMR * activityFactor;
}
else if (choice.equalsIgnoreCase("E")) && (userGender.equalsIgnoreCase("M"))
{
activityFactor = 2.1;
TDEE = userBMR * activityFactor;
}
else if (choice.equalsIgnoreCase("F")) && (userGender.equalsIgnoreCase("F"))
{
activityFactor = 2.2;
TDEE = userBMR * activityFactor;
}
else if (choice.equalsIgnoreCase("F")) && (userGender.equalsIgnoreCase("M"))
{
activityFactor = 2.4;
TDEE = userBMR * activityFactor;
}
else
{
System.out.println("You did not choose a valid menu option!");
}
System.out.println();
System.out.print("Name: " + firstName + " " + lastName);
System.out.println(" Gender: " + userGender);
System.out.print("BMR: " + userBMR);
System.out.println(" Activity Factor: " + activityFactor);
System.out.print("TDEE: " + TDEE);
}//end of main method
}//end of class
1 Answer
KRIS NIKOLAISEN
54,971 Points1) You have two closing brackets @ line 41
else if (choice.equalsIgnoreCase("B"))
{
activityFactor = 1.3;
TDEE = userBMR * activityFactor;
}
}
2) I added some additional parentheses so conditions were enclosed and was able to compile and run the program
import java.util.Scanner;
public class TDEE {
public static void main(String [] args){
double activityFactor = 0.0;
double TDEE = 0.0;
Scanner in = new Scanner(System.in);
System.out.print("Please enter your name: ");
String firstName = in.next();
String lastName = in.next();
System.out.println(firstName + " " + lastName);
System.out.print("Please enter your BMR: ");
double userBMR = in.nextDouble();
System.out.println(userBMR);
System.out.print("Please enter your gender (M/F): ");
String userGender = in.next();
System.out.println(userGender);
System.out.println();
System.out.println("Select your Activity Level");
System.out.println("[A] Resting (Sleeping, Reclining)");
System.out.println("[B] Sedentary (Minimal Movement)");
System.out.println("[C] Light (Sitting, Standing)");
System.out.println("[D] Moderate (Light Manual Labor, Dancing, Riding Bike)");
System.out.println("[E] Very Active (Team Sports, Hard Manual Labor)");
System.out.println("[F] Extremely Active (Full-Time Athlete, Heavy Manual Labor)");
System.out.println();
System.out.print("Enter the letter corresponding to your activity level: ");
String choice = in.next();
System.out.println(choice);
if(choice.equalsIgnoreCase("A"))
{
activityFactor = 1.0;
TDEE = userBMR * activityFactor;
}
else if (choice.equalsIgnoreCase("B"))
{
activityFactor = 1.3;
TDEE = userBMR * activityFactor;
}
else if ((choice.equalsIgnoreCase("C")) && (userGender.equalsIgnoreCase("F")))
{
activityFactor = 1.5;
TDEE = userBMR * activityFactor;
}
else if (((choice.equalsIgnoreCase("C")) && (userGender.equalsIgnoreCase("M"))) || ((choice.equalsIgnoreCase("D")) && (userGender.equalsIgnoreCase("F"))))
{
activityFactor = 1.6;
TDEE = userBMR * activityFactor;
}
else if ((choice.equalsIgnoreCase("D")) && (userGender.equalsIgnoreCase("M")))
{
activityFactor = 1.7;
TDEE = userBMR * activityFactor;
}
else if ((choice.equalsIgnoreCase("E")) && (userGender.equalsIgnoreCase("F")))
{
activityFactor = 1.9;
TDEE = userBMR * activityFactor;
}
else if ((choice.equalsIgnoreCase("E")) && (userGender.equalsIgnoreCase("M")))
{
activityFactor = 2.1;
TDEE = userBMR * activityFactor;
}
else if ((choice.equalsIgnoreCase("F")) && (userGender.equalsIgnoreCase("F")))
{
activityFactor = 2.2;
TDEE = userBMR * activityFactor;
}
else if ((choice.equalsIgnoreCase("F")) && (userGender.equalsIgnoreCase("M")))
{
activityFactor = 2.4;
TDEE = userBMR * activityFactor;
}
else
{
System.out.println("You did not choose a valid menu option!");
}
System.out.println();
System.out.print("Name: " + firstName + " " + lastName);
System.out.println(" Gender: " + userGender);
System.out.print("BMR: " + userBMR);
System.out.println(" Activity Factor: " + activityFactor);
System.out.print("TDEE: " + TDEE);
}//end of main method
}//end of class
Natalia Henderson
5,190 PointsNatalia Henderson
5,190 PointsOh my god, what a tiny mistake! Thank you so much :)