This week's book giveaway is in the OCAJP forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide 1Z0-808 and have Jeanne Boyarsky & Scott Selikoff on-line! See this thread for details.
Angilia Smith wrote:after the user enters the integers nothing happens... Logically I feel like my code makes sense, So I'm clueless to how to fix it!
You think your code should do one thing. Instead, it's doing something else. Obviously, that means one or more of your assumptions are wrong. So you can either stare at the code until you find your mistake (and once you gain experience, that's perfectly acceptable for certain problems, at least up to a point), or you can observe what your code is actually doing, and compare that to what you expect it to do. Where observation diverges from expectation, that's a sign that will point you toward your bogus assmption.
So add println() calls so that you can see what your code is actually doing--what's being executed and what various significant values are. For example:
(use whatever brace style you want, but be consistent).
The reason is that someday, you will want to put a println() statement inside the body of your if, or add more logic, or do SOMETHING else....and you will forget that the body is only one line when there aren't braces. And thing will get VERY weird VERY fast.
Further, writing a single line like that is so unusual that when I first looked at it, my thoughts went "Wait, that won't even compile...oh, I see what he did". When you are writing code, you should write it so that it makes sense to everyone. Following common conventions is a REALLY good idea.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Joined: Mar 13, 2013
I added the lines and found that its only reading the first number the user inputs. For example, if I input 100 78 45 -3, next always equals 100.
Angilia Smith wrote:I'm not sure what you're asking.
I'm asking why you expect it to process multiple user inputs when you're only ever telling it to read one of them.
I thought that while in a loop it would continue to the next number until it were no longer true. So in this case when next <= 0.
Correct, except that since your loop is while(next >= 0), it will keep going as long as next is >= 0, which means it will stop when x < 0, not when x <= 0. That's not your problem here, although it's something you'll need to pay attention to whenever you write a loop condition like that.
You have two problems that I can see. One is this:
Putting that semicolon there is the same as doing this:
You need to get rid of the semicolon after the while(...).
Your other problem, after you fix that, is that your loop body will either execute zero times or infinitely many times. This is what I was trying to hint at in my previous reply. You want the loop to execute as many times as there are user inputs, right? That means you have to read the user input for each execution of the loop, yes? But you're only reading one user input, and you use that same value for every iteration of the loop.
So if the value is sufficient for the loop to execute once, since you never change that value (because you're never reading any more user inputs), that means the loop will never stop executing.
If you want to process one user input per loop iteration, then you need to read one user input per loop iteration, but you're not doing that.