Jeroen Boot
Jeroen Boot

Reputation: 47

Variable might not have been initialized? Error

I am working on an app and it doesn't really works out at the moment. The app calculates how much calories you burned with walking the stairs. But when I want to round up the amount of calories it says Variable might not have been initialized. So I made the double kcal = 0 . But when I tested my app it just gave the number 0.0 as the calories I burned that day. Do you guys know how to resolve this problem?

Here is my code where I Declare the doubles:

double trap;
double kcal = 0;
double endkcal = Math.round( kcal * 100.0 ) / 100.0;
double keer;
double gewicht;
double nul = 0.13;
String eten = "Je kan nu Niks eten";

And here is the calculating code:

trap = Double.parseDouble(editText_trap.getText().toString());
gewicht = Double.parseDouble(editText_gewicht.getText().toString());
keer = gewicht * nul / 4;
kcal = keer * trap;
textView_kcal.setText(String.valueOf(endkcal) + "kcal");

Upvotes: 0

Views: 723

Answers (3)

Vivek Mishra
Vivek Mishra

Reputation: 5705

Initialize all of your double variables when you have declared them like this:

double trap=0.0;
double kcal =0.0;
double endkcal = Math.round( kcal * 100.0 ) / 100.0;
double keer=0.0;
double gewicht=0.0;
double nul = 0.13;

Upvotes: 1

Ivan Ivanov
Ivan Ivanov

Reputation: 912

You should use endkcal = Math.round( kcal * 100.0 ) / 100.0; after you calculate kcal to have correct calculation. Now it is normal to get 0.0 after calculation because kcal is 0.

Upvotes: 2

Overflow
Overflow

Reputation: 396

Apparently your endkcal variable is only initialized (to 0.0) but never updated.

Upvotes: 1

Related Questions