Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Boolean trouble

 
J. Moritz
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, would someone mind showing me what I'm doing wrong with my code?




The code won't compile unless I initialize the boolean rain variable to either true or false. I use a S.O.P statement to check the value of rain after the conditional/assignment of rain and no matter what is entered for weather rain always keeps the value of that it was initialized with.

Thanks for the help!
 
Joyce Lee
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

The local variable rain must be initialized before use. When this code is compiled, the following compilation error will be generated.


Lab14.java:32: variable rain might not have been initialized
if((temp >=35 && temp < 60) && rain==true)


This is because if weather is "no", the statement within the if-loop will not be run, and as a result, variable rain will be not initialized.

This code will work fine if variable rain is initialized to "false" instead of "true". And change "==" to "weather.equals("yes");


If it's raining and the temperature is 65, it'll show "Enjoy the golf course.". Is it an indoor one?

Joyce
[ October 20, 2004: Message edited by: Joyce Lee ]
 
Joyce Lee
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might want to take a look at the Java Language Specification: 4.5.5 Initial Values of Variables.

Joyce
 
Joyce Lee
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think this is the logic you want.



Joyce
 
J. Moritz
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Joyce,

Thank you so much for your help. I've had a little bit of "fuzzy" logic on this one and you've certainly got me in the right dircection. Your last reply was exactly what I needed to do to make this program work.

One day (maybe several hundred-thousand from now after I learn Java ) I hope to be able to contribute to the boards here like you have. You're most helpful. Thanks again
 
Jeff Bosch
Ranch Hand
Posts: 805
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All local variables, not just boolean types, need to be initialized before use.


Here's another tip:

Instead of waiting until you're a Java expert to contribute to JavaRanch, you can use JavaRanch to learn interactively. How? When someone posts a question, research the answer. Then, if you can explain the answer clearly, you have not only helped the original poster, you have also helped yourself because you now have one more piece of Java knowledge that you didn't have before. I know of few learning methods more effective than teaching.
 
J. Moritz
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jeff,

Good Point! Also, thanks for your information about local variables.

Take care,
JM
 
Jeff Bosch
Ranch Hand
Posts: 805
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're very welcome!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic