wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "error "variable not read"" Watch "error "variable not read"" New topic
Author

error "variable not read"

Bonnie Rossi
Greenhorn

Joined: Jan 01, 2011
Posts: 3
Hi folks,
Another "nooby" here (one week). Hope someone can help with my problem(s). I'm trying to write a simple beginner's program that allows input from the keyboard of 12 temperatures and then displays the highest temperature and the corresponding month. It's not going so good. My first problem occurs in line 14 where I get the error "highestMonth not read." I've changed everything around so many times I've lost count. Cannot figure out why it doesn't find the variable. Problems continue from there, but I can't go further until I figure this one out. Any help, comments, recommendations/pointers for ANY code would be greatly appreciated. Thanks in advance.

So far I have written:



Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14266
    
  21

Welcome to JavaRanch and happy new year!

Bonnie Rossi wrote:My first problem occurs in line 14 where I get the error "highestMonth not read."

Are you sure that's really an error or just a warning? The code doesn't look wrong; you're just not doing anything with the variable later on in your program. So your IDE is warning you that you're not reading the variable anywhere in your program, which is an indication that the program most likely isn't doing what you think it does.

Did you mean to put the value of highestMonth in the TemperatureClass object that you're creating in line 11?

Carefully and slowly look at your program line by line. Start at the beginning of the main() method. For each statement on each line, think about what it does. Make sure you understand it before you go to the next line. Reason about what's happening and why the program isn't doing what you want it to do. Don't go too quickly; if you're not sure, go back and carefully look again to understand how exactly it works.

In lines 13-25 you are reading in the temperature values and keeping track which value is the highest, and what the matching month number is. Where are these values stored when you reach line 25?

In lines 38 and 39 you are printing the highest temperature value and month value that you found. Where are you getting those values from there? Why don't you see the values that you kept track of in lines 13-25?

What is the role of the TemperatureClass? What is line 11 of your first code snippet for?

What exactly do the methods getHighestMonth() and getHighestTemp() in class TemperatureClass do? Where are these called in your first code snippet?

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39390
    
  28
You should never never write == true or == false. You have managed to copy it as = true at one point, which isn't what you want; it will assign the value to the boolean, and can cause logic errors. I think where you have got it, it only covers an exit call, so won't do that much harm

Avoid System.exit; it is unnecessary where you have it, and can cause nasty errors in threaded applications.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39390
    
  28
. . . and welcome to the Ranch (again)
Bonnie Rossi
Greenhorn

Joined: Jan 01, 2011
Posts: 3
Thanks. I practically started over from scratch with this program. I don't know if it's better or not, but it's shorter and almost works!
I really don't understand why the initial value for currentMonth has to start with 3 when it seems it should be a 2. Have I messed up another variable somewhere else? Also, I don't think the Q to quit works the way it's supposed to, but it does exit the program, albeit with a long list of red errors.


Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39390
    
  28
You can see it's an improvement when the whole thing gets shorter

Have you been told to put everything into the main method? The main method is really only there for starting off an application and its ideal size is one line.
Pleased to see you have get rid of the = true bit.
Please check your indentation. Get a text editor which supports automatic indentation (eg NotePad2, NotePad++, jEdit if on Windows, Gedit, Kate, on Linux), and write your code like thisCompile.CompileCompile. Yes, compile every time you write another 5 lines, and run every new method as soon as it is written. You find errors much faster like that.

Yes, it looks strange to add code in the middle of a block, but it gets your {} all paired off nicely above and below one another; if you have two }} above each other as in your lines 48-49 above, you can see you have your {} not paired correctly. that makes it a lot easier to understand some of the obscurer compiler errors. You will find suggestions about indentation here, which I prefer to the Sun/Oracle style suggestions. I shall leave you to find the compiler error in what I posted

Why are you starting your loop at month = 3? You obviously expect January and February to be very cold!

Move the instantiation for the Scanner to before the loop; otherwise you are instantiating it every time you enter a value. [It is possible the compiler can optimize that problem away, but that isn't a "beginning" topic.]
Copy the inner loop into a new method, without the bit about enter "Q". Get a little program working, then enlarge it. Much easier than trying to get something big working. Try your loop running through all twelve months, and instantiate the maximum value before the loop. Instantiate it to something so low (in the case of temperature, maybe -273.15°C) that you can be certain that the first value entered will be greater. Then run the loop for all twelve months. You can print "Enter temperature for month " + monthNumber and get a nice output on screen.
You don't need the two casts towards the end of the application.
Bonnie Rossi
Greenhorn

Joined: Jan 01, 2011
Posts: 3
I set the currentMonth variable to 3 because if I set it to 1 the loop ran 14 times instead of 12. I can't follow the logic very well obviously. I just tinkered until it hit 12 which turns out to be a 3. I should be able to have the highestMonth initialize at 1 and the currentMonth at 2 it seems, but this doesn't work. What have I missed?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39390
    
  28
Try the inner loop on its own. Forget all about "enter Q". Get the inner loop running correctly, then add "enter Q" later. Put a print statement at the beginning of the loop, so you can see how often it runs.
Don't tinker. Get the logic right for the inner loop, by writing the values on a piece of paper. You want a loop which runs from 1 to 12 (but beware: there are standard API classes where January = 0). So write a loop from 1 to 12, and get it to print "Enter temperature for month 1" for its first iteration.
Where you say "enter value or Q" and try to read that as a double, you are liable to get confused about whether you have entered Q or 123.4. You are omitting that bit for the time being, so you can correct that later.
 
Don't get me started about those stupid light bulbs.
 
subject: error "variable not read"