-2

I just want to know how to remove that initialized wgt error. Rest, I want my code to be basic and very simple.

//Program By Aryansh Malviya
//BMI Calculator
import java.util.Scanner;

public class BMICalc
{
    public static void main(String args[])
    {
        Scanner input = new Scanner(System.in);

        int cw;
        int ch;
        int bmi;
        int wgt;
        int hgt;
        int ct;

        System.out.print("\nNote: This program is not yet fully functional.\nThere might be issues regarding decimal values. \nProgram has not been equipped to accept or display in decimal values. \nThe program will be updated soon. \nSorry for the inconvinience.");
        System.out.print("Enter 1 if you want weight in Kilograms");
        System.out.print("Enter 2 if you want weight in Pounds");
        cw = input.nextInt();

        System.out.print("\nNote: If you choose to enter weight in Pounds, you'd have to enter height in Inches. Else, in meters");
        if(cw == 1)
        {
            System.out.print("\nEnter weight in Kilograms: ");
            wgt = input.nextInt();
        }

        else if(cw == 2)
        {
            System.out.print("\nEnter weight in Pounds: ");
            wgt = input.nextInt();
        }

         else
         {
            System.out.print("\nEnter a valid choice and try again!");
             }

        System.out.print("\nEnter your height: ");
        hgt = input.nextInt();

    bmi = wgt/(hgt * hgt);

    System.out.printf("\nYour weight is: %d", wgt);
    System.out.printf("\nYour height is: %d", hgt);
    System.out.printf("\n\nYour BMI is: %d", bmi);

    System.out.print("\nBMI VALUES");
    System.out.print("\nUnderweight: less than 18.5");
    System.out.print("\nNormal: between 18.5 and 24.9");
    System.out.print("\nOverweight: between 25 and 29.9");
    System.out.print("\nObese: 30 or greater");
}
}

When I compile the program I get the error that the variable wgt has not been initialized. Please tell me how to solve this problem.

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
4127157
  • 1,438
  • 2
  • 15
  • 28
  • 2
    Local variables need to be initialized, as the error message says. Initialize them to 0. – Jeroen Vannevel Jun 07 '14 at 13:23
  • possible duplicate of [Why are local variables not initialized in Java?](http://stackoverflow.com/questions/415687/why-are-local-variables-not-initialized-in-java) – TheMP Jun 07 '14 at 13:29

3 Answers3

0

You have two if statements - but if the user enters neither 1 nor 2 you end up in a part of the code where you try to use wgt without ever initializing it.

You should do two things:

First, initialize wgt when you first declare it (that makes the compiler error go away).

int wgt=0; 

would do it.

Next, make sure your program traps the case when cw is neither 1 nor 2. You could use a while loop with a flag, or some other mechanism. Right now, a wrong entry for cw will just keep on going (you print "enter a valid choice" but don't actually go back to the beginning…)

Here is how you fix all that:

import java.util.Scanner;

public class BMICalc
{
    public static void main(String args[])
    {
        Scanner input = new Scanner(System.in);

        int cw=0;
        int ch;
        double bmi;
        double wgt=0;
        double hgt;
        int ct;
        double BMIinchPoundFactor = 703;

        System.out.print("\nNote: This program is not yet fully functional.\nThere might be issues regarding decimal values. \nProgram has not been equipped to accept or display in decimal values. \nThe program will be updated soon. \nSorry for the inconvinience.");

        System.out.print("\nNote: If you choose to enter weight in Pounds, you'd have to enter height in Inches. Else, in meters");
        boolean validInput = false;
        while (!validInput) {
          System.out.print("\nEnter 1 if you want weight in Kilograms");
          System.out.print("\nEnter 2 if you want weight in Pounds\n");
          cw = input.nextInt();
          switch(cw) {
          case 1:
            System.out.print("\nEnter weight in Kilograms: ");
            wgt = input.nextDouble();
            validInput=true;
            break;
          case 2:
            System.out.print("\nEnter weight in Pounds: ");
            wgt = input.nextDouble();
            validInput=true;
            break;
          default:
            System.out.print("\nEnter a valid choice and try again!");
          }
        }
        System.out.print("\nEnter your height: ");
        hgt = input.nextDouble();

        bmi = wgt/(hgt * hgt);

        if(cw==2) bmi *= BMIinchPoundFactor;

        System.out.printf("\nYour weight is: %.0f", wgt);
        System.out.printf("\nYour height is: %.2f", hgt);
        System.out.printf("\n\nYour BMI is: %.1f", bmi);

        System.out.print("\nBMI VALUES");
        System.out.print("\nUnderweight: less than 18.5");
        System.out.print("\nNormal: between 18.5 and 24.9");
        System.out.print("\nOverweight: between 25 and 29.9");
        System.out.print("\nObese: 30 or greater\n\n");
    }
}

I took a few other liberties:

  1. Use double for things that might not be integer values
  2. Clean up the I/O (adding some '\n')
  3. Create a loop that keeps going until you give a valid input
  4. Properly compute BMI wen you give an input in pounds and inches

I'm sure it can be further improved…

Floris
  • 45,857
  • 6
  • 70
  • 122
0
  bmi = wgt/(hgt * hgt);

You are using wgt here and there is possibilities could not initialize if cw is neither 1 nor 2.

Raj
  • 942
  • 1
  • 7
  • 12
0

There is no guarantee that wgt has been initialized. For example, the user could enter '3' to the first question. You should consider and handle illegal inputs.