Rennade
Rennade

Reputation: 3

Java conditional statement/ initializing variable

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");
    }

Upvotes: 0

Views: 1355

Answers (5)

Fergus In London
Fergus In London

Reputation: 1203

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.

Upvotes: 0

Igwe Kalu
Igwe Kalu

Reputation: 14868

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.

Upvotes: 1

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

Upvotes: 0

Stamoulohta
Stamoulohta

Reputation: 659

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! ;)

Upvotes: 1

MByD
MByD

Reputation: 137322

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.

Upvotes: 2

Related Questions