Austin Austin - 2 months ago 9
Java Question

Java using else if to assign a value

I'm very new to programming and Java. I'm trying to use else if to assign a value to a variable if they selected the correct input. But whenever I try to compile it, it says it cannot find the variable.

import java.util.Scanner;

public class TDEE
{
public static void main(String[] args)
{

Scanner in = new Scanner(System.in);

double tdee = 0.0;

System.out.print("Please enter your name: ");
String name = in.next();

System.out.print("Please enter your BMR: ");
double bmr = in.nextDouble();

System.out.print("Please enter your Gender (M/F): ");
String gender = in.next();
System.out.println();

// providing menu items
System.out.println("Select your activity level: ");
System.out.println("[0] Resting (Sleeping. Reclining)");
System.out.println("[1] Sedentary (Minimal Movement)");
System.out.println("[2] Light (Sitting, Standing)");
System.out.println("[3] Moderate (Light Manual Labor, Dancing, Riding Bike)");
System.out.println("[4] Very Active (Team Sports, Hard Manual Labor)");
System.out.println("[5] Extremely Active (Full-time Athlete, Heavy Manual Labor)");
System.out.println();

// accept user choice with a Scanner class method
System.out.print("Enter the number corresponding to your activty level(0,1,2,3,4, or 5): ");
String choice = in.next();
System.out.println();

if(choice.equalsIgnoreCase("0"))
{
double activityFactor = 1.0;
}

else if(choice.equalsIgnoreCase("1"))
{
double activityFactor = 1.3;
}

else if(choice.equalsIgnoreCase("2") && "M".equals(gender))
{
double activityFactor = 1.6;
}

else if(choice.equalsIgnoreCase("2") && "F".equals(gender))
{
double activityFactor = 1.5;
}

else if(choice.equalsIgnoreCase("3") && "M".equals(gender))
{
double activityFactor = 1.7;
}

else if(choice.equalsIgnoreCase("3") && "F".equals(gender))
{
double activityFactor = 1.6;
}

else if(choice.equalsIgnoreCase("4") && "M".equals(gender))
{
double activityFactor = 2.1;
}

else if(choice.equalsIgnoreCase("4") && "F".equals(gender))
{
double activityFactor = 1.9;
}

else if(choice.equalsIgnoreCase("5") && "M".equals(gender))
{
double activityFactor = 2.4;
}

else if(choice.equalsIgnoreCase("5") && "F".equals(gender))
{
double activityFactor = 2.2;
}

else
{
System.out.println("You did not choose a valid manu option.");
}

tdee = bmr * activityFactor;

System.out.println();
System.out.println("Name: " + name + " Gender: " + gender);
System.out.println("BMR: " + bmr + " Activity Factor: " + activityFactor);
System.out.println("TDEE: " + tdee);
}
}

Answer

The reason it is not compiling is because of scoping. The variable you are declaring is inside an if statement. That gives its scope, ability to access it, to only within the if statement. In order to reference it outside the if statement, you must declare it within the scope you want to access it. Then you can assign values to it like as follows, activityFactor = 1.0;, within the if else statements. Where you declare your variables determines what can access them.

You have to declare double activityFactor outside of the if else statements. just include double activityFactor; above the statements and replace all double activityFactor with activityFactor, and it should compile.