• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem with if/else statements  RSS feed

 
Ranch Hand
Posts: 93
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

In-range Adder
Low end of range:
20
High end of range:
50
Enter data:
21
Enter data:
60
Enter data:
49
Enter data:
30
Enter data:
91
Enter data:
0
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?
 
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Borchgrevink wrote:but it's initialized on the line directly above that...

No, it's not. That line is inside the if part of the clause and therefore will not have executed if the else part is being executed.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way, I'm confused by your choice of:
The only value that satisfies that conditional expression is 0, so why aren't you just using:?
 
David Borchgrevink
Ranch Hand
Posts: 93
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:
David Borchgrevink wrote:but it's initialized on the line directly above that...

No, it's not. That line is inside the if part of the clause and therefore will not have executed if the else part is being executed.


so you're saying use a "}" at the end of all of the loop right? i tried that and then i got an "else without if" error :/
 
David Borchgrevink
Ranch Hand
Posts: 93
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:By the way, I'm confused by your choice of:
The only value that satisfies that conditional expression is 0, so why aren't you just using:?


i also believe i had exactly that for that if condition and it told me i needed a boolean statement so i changed it to be true or false but always equaling 0...
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Borchgrevink wrote:
so you're saying use a "}" at the end of all of the loop right?

No, I'm saying that the compiler 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.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Borchgrevink wrote:
i also believe i had exactly that for that if condition and it told me i needed a boolean statement

Then there was likely a syntax error in your expression. If you want to compare equal to 0, then "== 0" is the way to do it.
 
David Borchgrevink
Ranch Hand
Posts: 93
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:
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...
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 25
Eclipse IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
David Borchgrevink
Ranch Hand
Posts: 93
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is my advice (and I give this out a lot):

StopCoding (<--click and READ that page)

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.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Borchgrevink wrote:i also believe i had exactly that for that if condition and it told me i needed a boolean statement so i changed it to be true or false but always equaling 0...

My guess...and this is only a guess, is that you had


not
 
David Borchgrevink
Ranch Hand
Posts: 93
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
got it.

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!