-1
public class ClassName {

   public static void main(String[] args) {
   //code: depending on user input runs Methodname1();
   }

      public static void MethodName1 {

        double kgs;
        double totalIn;

        //code: do/while try/catch etc.


        double ImpToMetBmi;
        double InchToMtrH;

        InchToMtrH = totalIn*2.54/100;

        ImpToMetBmi = (kgs/(InchToMtrH*InchToMtrH);

        System.out.printf("\nYour BMI is: %.3f\n" ,ImpToMetBmi);
      }
}

Really sorry for the long and badly written code. I think all code/layout must be seen to figure out the problem.

Errors I'm getting: Exception...Uncompilable source code - variable totalIn might not have been initialized Exception...Uncompilable source code - variable kgs might not have been initialized

This formula worked before I inserted do/while try/catch statements for exception handling. I have spent hours reading about declaring and initilizing variables, local and class variables. I've tried a few different ways but nothing I try fixes the problem. I'm confused as to what is causing this and how to fix it. I'd like to figure this out and understand the solution. Where do I initialize 'totalIn' and 'kgs'? and What to I initialize them as? These varialbles are populated by values inputted by the user through Scanner if that makes any difference. Please help!

  • Could you please share some more details on how did you initialize the variables in the code. As per as what i see is you variable might have not initialized when in was being used. – Ashutosh Oct 23 '19 at 19:20
  • I only declared the variables and the formula worked until I inserted do/while and try/catch code. Then I got the initialization errors. I don't know where or what to initialize them as. Anything I try I get even more errors. I hope that makes sense. I'm new to Java and very confused -obviously :) – Julie Cohen Oct 23 '19 at 19:28
  • Sorry, that was a copy and paste error. My code in Netbeans is 'public static void MethodName1() {' – Julie Cohen Oct 23 '19 at 19:31

3 Answers3

0

public class ClassName {

   public static void main(String[] args) {
   //code: depending on user input runs Methodname1();
   }

      public static void MethodName1(double KGS, double TOTAL) {

        double kgs = KGS;
        double totalIn = TOTAL;

        //code: do/while try/catch etc.


        double ImperialToMetricBmi;
        double InchesToMtrHeight;

        InchesToMtrHeight = totalIn*2.54/100;

        ImperialToMetricBmi = (kgs/(InchesToMtrHeight*InchesToMtrHeight));

        System.out.printf("\nYour BMI is: %.3f\n" ,ImperialToMetricBmi);
      }
}
You could basically initialize both kgs and totalIn right where you have declared them but it would be better if the method takes those value as arguments(As of this point both the value never get initialized). Also then you would need to call the static method with both those arguments like

double value1 = 123.1;
double value2 =  24
MethodName1(value1, value2)

Further reading the question I realised that you might be trying to initialize the value inside a conditional statement or a loop . Understanding in simple terms what happens when the condition does to run the statement is not satisfied ? The answer is that the value never gets initialized which is the case happening here.

Nitin Khare
  • 147
  • 2
  • 10
  • why do you need to put the passed parameters to local variables ? you can directly use them right ! – Ashutosh Oct 23 '19 at 19:43
  • I've attempted some of your suggestions. I'm now in a situation where the program won't even run. I was trying to seek help without posting all of my code so people wouldn't have to read it all (I'm a beginner and I think it's badly written -it's my first program!). I will overwrite the original post with the full code because this is more complicated than I first thought. Hopefully you will still help even though the code is long and unprofessional :0 – Julie Cohen Oct 23 '19 at 20:35
0

Declaration of MethodName1 method is wrong. You missed argument section. Change it to public static void MethodName1().

Martin
  • 129
  • 1
  • 6
0

Here is an example that explains the cause you are getting and why you are getting that -

double test;
if( isTrue){
    test = 2.0d;`enter code here`
} 
// This will give you a error stating that test might have not initialized
double calculate = test * 5.0;

Reason is clear if condition in if block is true then the test value will be initialized with 2.0 other wise it will be uninitialized.

A quick fix to this might be initializing test to some value (may be 0).

Coming to you point, to initialize those variables you can do following things -

static double kgs;
static double totalIn;
public static void main(String args[]){
  Scanner sc = new Scanner(System.in);
  kgs= sc.nextDouble;
  totalIn = sc.nextDouble();
}


or pass them as method parameter like below -

public static void main(String args[]){
  Scanner sc = new Scanner(System.in);
  double kgs = sc.nextDouble;
  double totalIn = sc.nextDouble();
}

public void yourMethod(double kgs, double totalIn){
  // do whatever you want with above passed variables

}
Ashutosh
  • 917
  • 10
  • 19
  • Thank you @Ashutosh Kumar Sharma This worked for me: static double kgs; static double totalIn; public static void main(String args[]){ Scanner sc = new Scanner(System.in); kgs= sc.nextDouble; totalIn = sc.nextDouble(); } I'm still a little unsure why I only had to declare the variables and initialize them? They automatically initialize perhaps? – Julie Cohen Oct 23 '19 at 23:17
  • If you define a variable at class level then it will be initialized with its default value, even though that does not happens for lower scoped variables like method level variable.To your question you have to update you variables to the value you want to assign. – Ashutosh Oct 24 '19 at 04:15