here is the exercise with an example of how the program should run:
Write a program that asks the user for the low and high integer in a range of integers. The program then asks the user for integers to be added up. The program computes two sums:
The sum of integers that are in the range (inclusive),
and the sum of integers that are outside of the range.
The user signals the end of input with a 0.
Low end of range:
High end of range:
Sum of in range values: 100
Sum of out of range values: 151
here is my code:
I'm getting an error on the line with the first else if saying nextNum might not have been initialized. but it's initialized on the line directly above that...what am I missing?
David Borchgrevink wrote:
so you're saying use a "}" at the end of all of the loop right?
No, I'm saying that the compeller is correct when is says that nextNum will not have been initialized at that point. Why should nextNum be at that point?
This isn't a syntax issue, but a logic issue in your program.
i really don't know how else to ask what am i doing wrong :/ i think i see your point in saying that i closed the braces for the first if statement so the next else if would never see nextNum being initialized. if that's what you mean then i get that, BUT, when i move that close brace for the first if statement to the end of the whole loop i get an "else without if" error. i'm very very new to this and i don't know if i'm asking the right questions or making sense, i...ugh...
It's not about moving braces. It's not. So just forget that.
Look at your program. Really look at it. Step through the process of executing it.
You declare nextNum, without initialization at line 17. Then, at line 21, if count is zero, the if part is executed and nextNum is given a value.
But... BUT.... if count is not 0, the else part will execute (and the if part will NOT have executed), and nextNum remains uninitialized.
That's the problem.
So the question is, if count is not 0 (and it never will be because you set it to 1 at line 15), what should nextNum contain? Because it contains nothing. And that's what the compiler is complaining about.
What Bear said. You initialize count to 1, in which case the if statement "if ( count < 1 && count > -1 ) " is false, so the nextNum value doesn't get set to anything.
Whoever loves discipline loves knowledge, but whoever hates correction is stupid. (Proverbs 12:1 NIV)
posted 6 years ago
Brian Schuetz wrote:What Bear said. You initialize count to 1, in which case the if statement "if ( count < 1 && count > -1 ) " is false, so the nextNum value doesn't get set to anything.
okay so i'm not getting that error any longer. it compiles and runs, but incorrectly. the program asks for the first and second numbers, then asks for the first "Enter data"...so all is going well...and then it abruptly quits.
so i'm trying to conceptualize this in my head and i'm going okay if count > 0 (which it always will be) then start this...uh...loop... ....and now i see why this isn't working. should i not be using if/else at all, because if/else statements aren't going to loop right? if this is supposed to loop then i should be using while correct? i'm completely lost...
writing code is less about writing code, and more about THINKING. I probably spend around 70-80% of my time THINKING, and the rest coding. You have to understand what you are doing before you write line 1 of any program.
So, turn off your computer. Get some pencils and paper, and an eraser or seven. Use them a LOT. write down what needs to happen in ENGLISH (or whatever natural language you prefer). Revise them. make them simpler, and more clear. Get them to where a young child could follow them.
ONLY THEN should you consider writing any java code.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors