This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Grokking Bitcoin and have Kalle Rosenbaum on-line!
See this thread for details.
Win a copy of Grokking Bitcoin this week in the Cloud/Virtualization forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

For Statement Variable Initialization  RSS feed

 
Greenhorn
Posts: 13
1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Good Morning!

I am writing a program for a class that requests we use nested loops to request user input and compute the average rainfall over a user submitted number of years.

I am not looking for assistance with completing the assignment, more I have ran into an error that I cannot figure out how to resolve.

This is what I have so far:



The error I am getting is that month in the last output statement cannot be resolved to a variable, even though it's initialized in the for statement?

Just to test, I attempted to initialize the variable at the beginning of the code and as I expected, it told me I had a duplicate variable.

I though my issue was that the compiler cannot confirm the statements in the for loop will be executed, so it cannot assume the variable will be initialized (for example: if years is NOT greater than or equal to months - boolean in for) so I added an if statement that loops to require a result that will have that for statement boolean to initially be true. This did not resolve the issue, which I anticipated as I figured that was not explicit enough for the compiler.

It's weird because we also have an example program in the textbook to model after and it's written pretty much the same way without mentioning an issue with the variable created in the for.

I googled and found indirectly related posts that really did not clarify, so here I am.

Thanks in advance,
 
Marshal
Posts: 6685
464
BSD Linux Mac OS X VI Editor
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At line 19 you got semicolon at the end of loop declaration, so that finishes statement and "month" variable goes out of scope, remove that one as that is a mistake to do.
 
Liutauras Vilda
Marshal
Posts: 6685
464
BSD Linux Mac OS X VI Editor
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
C Robinson, wanted to point out one thing, the way you described your problem and explained what issues you ran into - it was exemplary.

Being able to describe clearly a scenario which you attempted, what have you tried already, what went wrong, and what did you expect actually is a good indication that you are heading correct way as a programmer - that's an important skill.

We all do such mistakes which sometimes is very hard to find, so yours was one of those.

Actually one more thing. Please also pay the same attention to the comments you write in your code. As they are - they are pretty useless, because they don't tell anything useful or anything what we couldn't acquire from the code already. And I think if you were not have those written, you might would have spot that unnecessary semi-colon yourself, because your eyes wouldn't be hooked by the comment which is in the following line.
 
Marshal
Posts: 64157
215
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet Souris suggests that sort of comment may be a reminder to the student about how to write the code. In which case such comments are good when they are first writte, but become less useful after the code is filled n.
 
C Robinson
Greenhorn
Posts: 13
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:C Robinson, wanted to point out one thing, the way you described your problem and explained what issues you ran into - it was exemplary.

Being able to describe clearly a scenario which you attempted, what have you tried already, what went wrong, and what did you expect actually is a good indication that you are heading correct way as a programmer - that's an important skill.



Thank you very much for the encouragement!!

Liutauras Vilda wrote:Actually one more thing. Please also pay the same attention to the comments you write in your code. As they are - they are pretty useless, because they don't tell anything useful or anything what we couldn't acquire from the code already. And I think if you were not have those written, you might would have spot that unnecessary semi-colon yourself, because your eyes wouldn't be hooked by the comment which is in the following line.



I appreciate the insight and feedback too. Is the concern more that there are too many overall useless comments or that I am not including comments that would be needed? I am very early in my development, and my class requires you comment everything, ad nauseam, or risk losing points. While I may not include them in my own programs, only where I anticipate some explanation is needed, I have a 98 in the class at mid-term, so I'm going to toe the line.

As always, I learn a lot every time I come here, it's really awesome of this community to be so available to support each other and beginners like me!
 
C Robinson
Greenhorn
Posts: 13
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:At line 19 you got semicolon at the end of loop declaration, so that finishes statement and "month" variable goes out of scope, remove that one as that is a mistake to do.



Also,



I knew this rule, just didn't see it, probably because of the excessive commenting, as you say.
Staff note (Knute Snortum):

When you use the image tags, be sure that the URL points to an actual image and not a web page.

 
Campbell Ritchie
Marshal
Posts: 64157
215
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

C Robinson wrote:. . . just didn't see it, . . .

Everybody ellse makes that mistake, so you mustn't let it worry you.

If you read the code out aloud and it isn't immediately obvious what any part of it means, consider a comment to explain it. Like this:-But changing the code slightly is often a much better solution:-...and slighty more error‑resistant. If you set up MONTHS_IN_YEAR correctly once it will be correct everywhere you use it with no risk of writing 13 instead of 12.The following comment doesn't tell you anything you can't tell from the code:-There is no need for comments here...because everybody starts counting from 0. The following however counts as “hard to debug”:-...because nobody knows where 1 2 and 3 have gone. Ths sort of thing is much more useful:-
 
Bartender
Posts: 700
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Think about how you name your variables. When you do this: the variable "years" is no longer the number of years, but is now the number of months. This can lead to great confusion later on.
And why do you declare months to be a double, rather than an int? If it can never be anything other than an integer, declare it as such.
On the other hand, if it can be a non-integer, then you might run into trouble when you mix ints with doubles, as in
If you think months is 1200, but it is really 1199.999999, then your loop will not iterate the correct number of times.
 
Well behaved women rarely make history - Eleanor Roosevelt. tiny ad:
Create Edit Print & Convert PDF Using Free API with Java
https://coderanch.com/wiki/703735/Create-Convert-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!