• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Please Help

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have an assignment where I need to compute the Body Mass. My problem is I cannot figure out why the same line of code will compile OK if it's placed inside of an if statement, but when I move it outside of the if statement, I get a compiler error of: "variable bodyIndex might not have been initialized". Any ideas?

the line of code in question is: System.out.printf("\nYour BMI value is %d",bodyIndex);

if (method == 1)
{
System.out.print("Please enter your weight in pounds: ");
userWeight = input.nextInt();
System.out.print("Please enter your height in inches: ");
userHeight = input.nextInt();
bodyIndex =((userWeight * 703) / (userHeight * userHeight));
System.out.printf("\nYour BMI value is %d",bodyIndex); // <---- Here it is in the if statement. This will compile OK.
}

Once I take it out of the if statement, I get an error compiling.


Thank you in advance for any assistance given.
 
Marshal
Posts: 7029
1341
IntelliJ IDE jQuery Eclipse IDE Postgres Database Tomcat Server Chrome Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Local variables must be initialized before you can access them. As we can predict here, bodyIndex is a local variable and it will be initialized if and only if method==1
Statements below the if block are executable even though the if condition fails, and bodyIndex is not initialized.
 
lowercase baba
Posts: 13082
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it's telling you what the problem is. What happens if you're code doesn't enter the if-block? What value will the bodyIndex variable be intialized to?

if you leave the line inside the if-block, then there is no way to use it without it getting initialized first.

You could probably solve this by initializing the int (I assume it's an int)

 
Mike Nguyen
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much. That was it
 
Marshal
Posts: 76873
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch

Please avoid thread titles like "Please Help" as explained here.
 
Always! Wait. Never. Shut up. Look at this tiny ad.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic