title: Java BMI program
body: <p>The first part of the code is working, the program takes inputs as height and weight as displays an output message. When I press n I can go to the second part and take inputs for height in inches and the weight in pounds. As soon as I take them the program outputs the resulting bmi is: Nan. and the variable bmi might not have been initialized. i want to convert the height and weight from inches and pounds to meters and then show the resulting bmi This is my code:</p>
  <pre><code>package uly14th;
  import java.util.Scanner;
  
  public class BmiCalculator {
  
      private static float BMI2;
      public static void main(String[] args) {
          char Y; 
          char y; 
          char Q;
          char n;
          char N;
          float heightInMeters, weightInKilograms, BMI;
          float heightInInches, weightInpounds;
          float heightInMeters2 = 0, weightInKilograms2 = 0;
          
          Scanner input = new Scanner(System.in);
          System.out.println(&quot;Please state whether you are going to use kilograms &amp; meters or, inches &amp; pounds&quot;);
          System.out.println(&quot;If you want to use the former please press Y or if you want to use the latter please press N&quot;);
          Q = input.next().charAt(0);
          if(Q == 'y' || Q == 'Y' ) {
              System.out.println(&quot;Please enter the height in meters: &quot;);
              heightInMeters = input.nextFloat();
              System.out.println(&quot; and weight in kilograms: &quot;); 
              weightInKilograms = input.nextFloat();
              
              BMI =  weightInKilograms / (heightInMeters * heightInMeters);
             
              System.out.println(&quot;The resulting BMI of the person is: &quot; +BMI);
              if (BMI &lt; 18.5){
                  System.out.println(&quot;The personis underweight&quot;);
              }
              else if ((BMI&lt;=25.0) &amp;&amp; (BMI &gt;= 18.5)){
                  System.out.println(&quot;The person is normal&quot;);
              }
              else if ((BMI &gt;= 25.0) &amp;&amp; (BMI &lt;=30.0 )){
                  System.out.println(&quot;The personis obese&quot;);
              }
              else if(BMI&gt;=30.0) {
                  System.out.println(&quot;The person is obese and should exercise&quot;);
              }
          }
          if ((Q == 'n') || (Q == 'N')){
              System.out.println(&quot;Please enter the height in inches: &quot;);
              
              heightInInches = input.nextFloat();
              System.out.println(&quot;Please enter the weight in pounds: &quot;);
              weightInpounds = input.nextFloat();
              
              heightInInches = 0.0254f * heightInMeters2;
              weightInpounds = 0.453592f * weightInKilograms2;
            
              BMI =  weightInKilograms / (heightInMeters * heightInMeters);
              
              System.out.println(&quot;The resulting BMI of the person is: &quot; +BMI); 
              
              if (BMI &lt; 18.5){
                  System.out.println(&quot;The personis underweight&quot;);
              }
              else if ((BMI&lt;=25.0) &amp;&amp; (BMI &gt;= 18.5)){
                  System.out.println(&quot;The person is normal&quot;);
              }
              else if ((BMI &gt;= 25.0) &amp;&amp; (BMI &lt;=30.0 )){
                  System.out.println(&quot;The personis obese&quot;);
              }
              else if(BMI&gt;=30.0) {
                  System.out.println(&quot;The person is obese and should exercise&quot;);
              }
          }
          else {
              System.out.println(&quot;Please make sure you run the program again and then only use the characters n,N,y,Y. &quot;);
          }   
      }        
  }
  </code></pre>
  <p>This is the error message: Exception in thread &quot;main&quot; java.lang.RuntimeException: Uncompilable source code - variable BMI might not have been initialized
  at uly14th.BmiCalculator.main(BmiCalculator.java:82)</p>