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 trial
Staffan Persson
3,554 PointsFeedback on my code
Hey guys! I just started learning java a few days ago. I did this code for practice and would like some feedback. The code is working but would like to know if there's something that could be coded in a better way.
import java.io.Console;
class macroCalculator {
public static void main(String args[]) {
Console console = System.console();
double bmr, tdee, suggested, aggressive, weight, length, activity, bulkCautious, bulkNormal;
String weightAsString, lengthAsString, ageAsString, activityAsString, gender, formula;
int age;
bmr = 0;
tdee = 0;
suggested = 0;
aggressive =0;
bulkCautious = 0;
bulkNormal = 0;
formula = console.readLine (" which formula would you like to use? Answer A or B\n A) Harris-Benedict\n B) Mifflin-St-Jeor\n ");
gender = console.readLine ("Are you male or female? ");
weightAsString = console.readLine("Enter your weight (kg): ");
lengthAsString = console.readLine("Enter your length (cm): ");
ageAsString = console.readLine("Enter your age: ");
weight = Double.parseDouble(weightAsString);
length = Double.parseDouble(lengthAsString);
age = Integer.parseInt(ageAsString);
console.printf("Choose your activity factor.\n 1.2 = Sedentary (little or no exercise).\n 1.3-1.4 = Lightly active (light exercise/sports 1-3 days/week). \n 1.55 = Moderatetely active (moderate exercise/sports 3-5 days/week).\n 1.725 = Very active (hard exercise/sports 6-7 days a week).\n 1.9 = Extra active (very hard exercise/sports & physical job or 2x training).\n");
activityAsString = console.readLine("Enter your activity factor: ");
activity = Double.parseDouble(activityAsString);
boolean male = (gender.equalsIgnoreCase("Male"));
boolean female = (gender.equalsIgnoreCase("female"));
boolean a = (formula.equalsIgnoreCase("a"));
boolean b = (formula.equalsIgnoreCase("b"));
if (male && a) {
bmr = 66 + (13.7 * weight) + (5 * length) - (6.76 * age);
tdee = bmr * activity;
suggested = tdee * 0.85;
aggressive = tdee * 0.8;
bulkCautious = tdee * 1.05;
bulkNormal = tdee * 1.1;
}
else if (female && a) {
bmr = 655 + (9.6 * weight) + (1.8 * length) - (4.7 * age);
tdee = bmr * activity;
suggested = tdee * 0.85;
aggressive = tdee * 0.8;
bulkCautious = tdee * 1.05;
bulkNormal = tdee * 1.1;
}
else if (male && b) {
bmr = (9.99 * weight) + (6.25 * length) - (4.92 * age) + 5;
tdee = bmr * activity;
suggested = tdee * 0.85;
aggressive = tdee * 0.8;
bulkCautious = tdee * 1.05;
bulkNormal = tdee * 1.1;
}
else if (female && b) {
bmr = (9.99 * weight) + (6.25 * length) - (4.92 * age) -161;
tdee = bmr * activity;
suggested = tdee * 0.85;
aggressive = tdee * 0.8;
bulkCautious = tdee * 1.05;
bulkNormal = tdee * 1.1;
}
System.out.println();
System.out.println("Your BMR is " + Math.round(bmr));
System.out.println();
System.out.println("Your TDEE is " + Math.round(tdee));
System.out.println();
System.out.println("For weightloss your suggested kcal intake is " + Math.round(suggested));
System.out.println();
System.out.println("For aggressive weightloss your kcal intake is " + Math.round(aggressive));
System.out.println();
System.out.println("For a cautious bulk your kcal intake is " + Math.round(bulkCautious));
System.out.println();
System.out.println("For a normal bulk your kcal intake is " + Math.round(bulkNormal));
System.out.println();
}
}
2 Answers
Staffan Persson
3,554 PointsSorry
Derek Markman
16,291 PointsI ran your program a few times, nice job man! It seems to do what it was intended to do.
As far as your code goes, I think that it is well written, if I was being a strickler I would say to capitalize the "M" in "macro" for the class declaration.(Personally, I'm very picky with little things like that lol). I'm just gonna trust you on the math lol, seeing as how I know very little about the math behind macro nutrient calculators.
This would be fun to integrate in a little program I've been improving upon in my spare time (If you want you can post your email and I'll email you about it).
One major issue I noticed with your program is, when you're parsing the input from your readLine methods. If the user were to type in a string instead of a number, like you were expecting, your program would throw an ugly error message at the user. To fix this issue you could use a try-catch block. You wrap the code that you want to "try" inside the try block, then in your catch block you can write a nice error message to the user if the exception that you're testing for is caught, and/or print the stack trace.
Also, the print statements at the end can be cleaned up a little by using new lines, instead of empty print statements.
import java.io.Console;
class macroCalculator {
public static void main(String args[]) {
try { //beginning of try block
Console console = System.console();
double bmr, tdee, suggested, aggressive, weight, length, activity, bulkCautious, bulkNormal;
String weightAsString, lengthAsString, ageAsString, activityAsString, gender, formula;
int age;
bmr = 0;
tdee = 0;
suggested = 0;
aggressive =0;
bulkCautious = 0;
bulkNormal = 0;
formula = console.readLine (" which formula would you like to use? Answer A or B\n A) Harris-Benedict\n B) Mifflin-St-Jeor\n ");
gender = console.readLine ("Are you male or female? ");
weightAsString = console.readLine("Enter your weight (kg): ");
lengthAsString = console.readLine("Enter your length (cm): ");
ageAsString = console.readLine("Enter your age: ");
weight = Double.parseDouble(weightAsString);
length = Double.parseDouble(lengthAsString);
age = Integer.parseInt(ageAsString);
console.printf("Choose your activity factor.\n 1.2 = Sedentary (little or no exercise).\n 1.3-1.4 = Lightly active (light exercise/sports 1-3 days/week). \n 1.55 = Moderatetely active (moderate exercise/sports 3-5 days/week).\n 1.725 = Very active (hard exercise/sports 6-7 days a week).\n 1.9 = Extra active (very hard exercise/sports & physical job or 2x training).\n");
activityAsString = console.readLine("Enter your activity factor: ");
activity = Double.parseDouble(activityAsString);
boolean male = (gender.equalsIgnoreCase("Male"));
boolean female = (gender.equalsIgnoreCase("female"));
boolean a = (formula.equalsIgnoreCase("a"));
boolean b = (formula.equalsIgnoreCase("b"));
if (male && a) {
bmr = 66 + (13.7 * weight) + (5 * length) - (6.76 * age);
tdee = bmr * activity;
suggested = tdee * 0.85;
aggressive = tdee * 0.8;
bulkCautious = tdee * 1.05;
bulkNormal = tdee * 1.1;
}
else if (female && a) {
bmr = 655 + (9.6 * weight) + (1.8 * length) - (4.7 * age);
tdee = bmr * activity;
suggested = tdee * 0.85;
aggressive = tdee * 0.8;
bulkCautious = tdee * 1.05;
bulkNormal = tdee * 1.1;
}
else if (male && b) {
bmr = (9.99 * weight) + (6.25 * length) - (4.92 * age) + 5;
tdee = bmr * activity;
suggested = tdee * 0.85;
aggressive = tdee * 0.8;
bulkCautious = tdee * 1.05;
bulkNormal = tdee * 1.1;
}
else if (female && b) {
bmr = (9.99 * weight) + (6.25 * length) - (4.92 * age) -161;
tdee = bmr * activity;
suggested = tdee * 0.85;
aggressive = tdee * 0.8;
bulkCautious = tdee * 1.05;
bulkNormal = tdee * 1.1;
}
System.out.println("\n Your BMR is " + Math.round(bmr) + "\n");
System.out.println("Your TDEE is " + Math.round(tdee) + "\n");
System.out.println("For weightloss your suggested kcal intake is " + Math.round(suggested) + "\n");
System.out.println("For aggressive weightloss your kcal intake is " + Math.round(aggressive) + "\n");
System.out.println("For a cautious bulk your kcal intake is " + Math.round(bulkCautious) + "\n");
System.out.println("For a normal bulk your kcal intake is " + Math.round(bulkNormal) + "\n");
} catch (NumberFormatException nfe) { //catch block for the NumberFormatException
System.out.println("Sorry your input wasn't valid. Try again.");
//nfe.printStackTrace(); - this is optional, but is used in most catch blocks.
}
}
}
I don't know how much you experience with java you have total, not just here on treehouse, but if you don't already know, the catch block will catch the NumberFormatException (if one occurs), and will display the message "Sorry your input wasn't valid. Try again." Instead of an ugly error message.
Otherwise, great job!
Staffan Persson
3,554 PointsThanks!
It's been about a week I guess since I first opened a book on java. Since then I've read 1 chapter and completed the java basics course along with experimenting some on my own. Had no prior experience in programming so this is the first time I've heard of try-catch block.
My email is staffan.persson@hotmail.com
Derek Markman
16,291 PointsDerek Markman
16,291 PointsIf you would like others to give you feedback on your code I suggest you edit your thread message. Right now your code is very hard to read as is.
1. Re-copy the code from your ide/text editor.
2. Re-paste and put ALL the code in between the 2 sets of backticks, you can paste java code by writing "java"(exclude the quotation marks) after the first set of backticks.
3. Make sure there is an empty line in between any text you want to display before, or after your code.
If you did it correctly it should look like: