0

Currently this is my code, and I need to display " Please enter a valid choice" when the user don't pick A,B,C,D,E or F as their choices. The problem is if I put the statement " Please enter a valid...." on the "else" conditional, Java would ask me to initialized the variable ActivityFactor as there will not be one if the user don't select the correct choice. Anyone know how I can fix this? Or any idea how I should code a program to do such?

if((inGender.equalsIgnoreCase("M") ||(inGender.equalsIgnoreCase ("F"))) && inActivity.equalsIgnoreCase("A"))
            ActivityFactor = 1.0;

        else if ((inGender.equalsIgnoreCase("M") ||(inGender.equalsIgnoreCase ("F"))) && inActivity.equalsIgnoreCase("B"))
        ActivityFactor = 1.3;

        else if (inGender.equalsIgnoreCase("M") && inActivity.equalsIgnoreCase("C"))
        ActivityFactor = 1.6;
        else if (inGender.equalsIgnoreCase("F") && inActivity.equalsIgnoreCase("C"))
        ActivityFactor = 1.5;
        else if (inGender.equalsIgnoreCase("M") && inActivity.equalsIgnoreCase("D"))
        ActivityFactor = 1.7;
        else if (inGender.equalsIgnoreCase("F") && inActivity.equalsIgnoreCase("D"))
        ActivityFactor = 1.6;
        else if (inGender.equalsIgnoreCase("M") && inActivity.equalsIgnoreCase("E"))
        ActivityFactor = 2.1;
        else if (inGender.equalsIgnoreCase("F") && inActivity.equalsIgnoreCase("E"))
        ActivityFactor = 1.9;
        else if (inGender.equalsIgnoreCase("M") && inActivity.equalsIgnoreCase("F"))
        ActivityFactor = 2.4;
        else if (inGender.equalsIgnoreCase("F") && inActivity.equalsIgnoreCase("F"))
        ActivityFactor = 2.2;
        else
        {
       ActivityFactor = -1;

    }

    //After
    if(ActivityFactor != -1){
     tdee = (nBMR * ActivityFactor);
     System.out.println(tdee);}
    else
   { System.out.println("Please enter a valid choice");
    }

5 Answers 5

2

If non of the conditions in the if statements is true, then you don't assign anything to ActivityFactor, and it is not initialized when used in the line double TDEE = (nBMR * ActivityFactor);.

Either initialize it before the code you've shown here, give it a default value in the last case, or loop until you get a valid value.

Sign up to request clarification or add additional context in comments.

2 Comments

Is it possible for you to give me an idea on how to create a program where the user is asked to ender their gender, and BMR. Then select a choice of activity ( From A to F, each choice assign a different activity factor), if the user don't select a valid choice, then display " Please enter a valid choice" and if they do enter a valid choice, the factor is * with the bmr. Thanks
Even if one of the conditions in the if-else statement is true, you can't know it before hand. ActivityFactor won't be considered initialized and the code won't compile.
1

initialise ActivityFactor to an usual value before your conditional.

For example you may do this:

// knowing that it can never be -1
// so if that value remains, you know that user entered wrong letter
ActivityFactor = -1

// then the conditional begins
if((inGender.equalsIgnoreCase("M") ||(inGender.equalsIgnoreCase ("F"))) && inActivity.equalsIgnoreCase("A"))
...

// after conditional...
if(activityFactor != -1){
    double TDEE = (nBMR * ActivityFactor);
}

By the way, I suggest you use 'activityFactor' instead of ActivityFactor.

7 Comments

If I initialized it to -1 wouldn't it still carry out the double TDEE = (nBMR * ActivityFactor);? Thanks
before carrying that out test if it's equal to -1, if it is then don't carry it out. I'll edit my answer to demonstrate.
Thank you!Can you look at my code again and tell me if it similar to what you're talking about ?
yes that's similar. The idea is this, the value -1 works as an error code. That should work for now I bet. Eventually, you'll learn to do it in other interesting ways.
This is very much creating a later (and identical) problem to solve an immediate one. This will create problems if you're going to later rely on TDEE though - you'll be back to square one as TDEE wont be initialised/exist. Much better solution is to get valid input prior to continuing execution.
|
1

Either initialize your variable before the loop, or place the whole loop inside a function and then do something like:

double TDEE = (nBMR * getActivityFactor());

Also, have a look at this : http://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html

happy coding! ;)

Comments

0

You should do two things:

  1. Enclose the logic in method
  2. Throw an exception if argument do not match the method logic.

You can solve this problem throwing an exception that you will catch.

private double getTDEE (String inGender, String inActivity) {

   //logic
   else {
     throw new IllegalArgumentException("Please enter a valid choice");
   }

  return (nBMR * ActivityFactor);
}

Exception tutorial

Comments

0

As you're already aware, the problem with your code is that execution continues regardless of whether the user has entered valid input. Due to this, much refactoring is due - I would also attempt to make the conditional statements a little bit prettier; but that's personal preference.

Possible solutions:

a) Use a loop - Then break out of the loop when the user has entered satisfactory input..

while( true ){ 
   /* get input from the user */

   /* run through validation checks... 
        and -break- out of the loop when they're satisfied */
}
/* do calculations here */

b) Use a function to abstract all this logic away.. (as Psyclops suggests)

Personally, I would use a combination of these approaches - have all of this logic extracted away in to a function which returns false when no valid input is entered, and then use a construct like while(! yourFunction() ) to simply loop through it until it's completed. You could using passing by reference to avoid having to use the return type for anything other than a boolean value.

I wouldnt initialise the variable before the loop! This just means the programme will continue to execute; however it wont have any appropriate data in there - which is potentially worse than the application just crashing.

I haven't exactly gifted you the answer in code - but hopefully it's a starting point to allow you to think about how to conceptually compose/design such a solution. That's generally the hardest part.. ;) Good luck.

2 Comments

Thank you! When you say to make the conditional statement prettier, could you tell me how?
@user1906857 - actually that may have been a mistake on my end, as I was viewing this post on my phone! I generally avoid long conditional structures like that though, preferring to generate some form of alternate solution - perhaps with the use of an enumeration etc. That really is personal preference though.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.