aspose file tools*
The moose likes Beginning Java and the fly likes find the smallest, largest, and average Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "find the smallest, largest, and average" Watch "find the smallest, largest, and average" New topic
Author

find the smallest, largest, and average

Angilia Smith
Greenhorn

Joined: Mar 13, 2013
Posts: 4
I can't get my code to work!! Please help. I need to find the smallest, largest, and average. I'm VERY new to all of this, so anything will help.

Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Hi, and welcome to the Ranch!

Here are a couple of tips to help you get the most out of the awesome help that's available here:

1. TellTheDetails(⇐click) and ItDoesntWorkIsUseless(⇐click). We need to know exactly what the problem is in order to effectively help you.

2. When posting code, please UseCodeTags(⇐click). I added them for you this time. See how much more readable it is?

So let us know more about what the problem is, and somebody will be happy to help.
Angilia Smith
Greenhorn

Joined: Mar 13, 2013
Posts: 4
I apologize. I can get it to run however, 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!
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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:

fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11475
    
  16

Don't ever write lines like this:



Always do this:

(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
Angilia Smith
Greenhorn

Joined: Mar 13, 2013
Posts: 4
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.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Angilia Smith wrote:I added the lines and found that its only reading the first number the user inputs.


Hmmm... So it seems to be ignoring the code you wrote where you tell it to read more. Point out where you're telling it to read more, so that we can help you figure out why it's not working.
Angilia Smith
Greenhorn

Joined: Mar 13, 2013
Posts: 4
I'm not sure what you're asking. 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.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: find the smallest, largest, and average