aspose file tools*
The moose likes Beginning Java and the fly likes Boolean trouble Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Boolean trouble" Watch "Boolean trouble" New topic
Author

Boolean trouble

J. Moritz
Greenhorn

Joined: Oct 20, 2004
Posts: 5
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

Joined: Jul 11, 2003
Posts: 1392
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

Joined: Jul 11, 2003
Posts: 1392
You might want to take a look at the Java Language Specification: 4.5.5 Initial Values of Variables.

Joyce
Joyce Lee
Ranch Hand

Joined: Jul 11, 2003
Posts: 1392
I think this is the logic you want.



Joyce
J. Moritz
Greenhorn

Joined: Oct 20, 2004
Posts: 5
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

Joined: Jul 30, 2003
Posts: 805
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.


Give a man a fish, he'll eat for one day. Teach a man to fish, he'll drink all your beer.
Cheers, Jeff (SCJP 1.4, SCJD in progress, if you can call that progress...)
J. Moritz
Greenhorn

Joined: Oct 20, 2004
Posts: 5
Hi Jeff,

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

Take care,
JM
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 805
You're very welcome!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Boolean trouble