aspose file tools*
The moose likes Beginning Java and the fly likes Help with Min, Max, 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 "Help with Min, Max, and Average" Watch "Help with Min, Max, and Average" New topic
Author

Help with Min, Max, and Average

Angela Hill
Greenhorn

Joined: Jun 13, 2012
Posts: 19
Hello All,
I would love if anybody could please help me with coding the math part of Min Max and Avg. I am having a difficult time trying to call to my variables to get the correct answer.
here is my code so far...


Also, I cannot get my loop to stop for my avg. it says infinity. lol
Daryl Cofer
Greenhorn

Joined: Jun 20, 2012
Posts: 19

Where do you increment your count variable?

Edit: Nevermind I see it. Try moving your count++ outside of your assignment for Avg.

Count++
Avg = Num+Num/Count

Edit: Also take into consideration order of operations. You want to add the two numbers before you divide by the count so:

Count++
Avg = (Num+Num)/Count
Angela Hill
Greenhorn

Joined: Jun 13, 2012
Posts: 19
this is my updated code. However when I put in an input say 1 2 3. my output is:Please enter your numbers.
1 2 3
The Average value is: 1.0
Maximum: 1
Minimum: 0
which is obviously not correct.

Daryl Cofer
Greenhorn

Joined: Jun 20, 2012
Posts: 19



That should do it. All I did was add a while loop that checks to see if there are any more tokens to look through.
Angela Hill
Greenhorn

Joined: Jun 13, 2012
Posts: 19
Thank you so much!!!
One last thing when I do the input the min comes out as 0 every time any suggestions to prevent this?
Daryl Cofer
Greenhorn

Joined: Jun 20, 2012
Posts: 19

Change the values when you initialize your min and max values.

Integer.MAX_VALUE and MIN_VALUE are built-in Java constants. They are the largest and smallest values that can be represented in integer form. So by setting the Max to Integer.MIN_VALUE, any number you enter is automatically going to re-assign the Max variable. Same goes with the Min variable.

Angela Hill
Greenhorn

Joined: Jun 13, 2012
Posts: 19
Ah you are a saint I would have never of thought of that...then again i'm pretty new at all this lol Thanks again
Daryl Cofer
Greenhorn

Joined: Jun 20, 2012
Posts: 19

Happy to help!
 
Don't get me started about those stupid light bulbs.
 
subject: Help with Min, Max, and Average
 
Similar Threads
StringTokenizer
Doubt about StringTokenizer !!
Java Newbie in need of assistance, reading lines from text file and calculating averages.
Extract all integers from a text file, how to do?
read specific line from text file