This week's book giveaway is in the JDBC forum. We're giving away four copies of Make it so: Java DB Connections & Transactions and have Marcho Behler on-line! See this thread for details.

Working on a programming question from "The Art and Science of Java". I need to allow the user to input any amount of numbers they want and then when they finish produce the average of their numbers. I wrote a program where I add all of the numbers input but then I'm stuck on the division part because I don't know how many different numbers they input? Any help appreciated. Here is what I have so far:

public void run() {
double average = 0.0;
while (true) {
double score = readDouble("Enter grade: ");
if (score == SENTINEL) break;
average += score / ?;

}

println("The average is: " + average);
}
private static final int SENTINEL = -1;
}

"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.

Wouter Oet wrote:sumOfNumbers / numberOfNumbers = average

Good luck.

Since the user can input any amount of numbers how can I know the numberOfNumbers or have my program calculate that? If I knew in advance how many they would input then I would, but if not?

fred rosenberger wrote:the first thing you should always do is work out how you'd do something by hand - i.e. using paper and pencil.

so, assume I was going to read a list of numbers, and I want you to tell me the average of them once I tell you i'm done. How would you do that?

I would count how many numbers you read off and then divide by that number. Contemplating this I am trying to think of a method to figure out how many numbers the user input but am at a loss. I am going to go back and read the chapter again and see if I can figure this out.

You need another variable: a counter that keeps track of how many numbers have been entered. Remember that any variable can be added to itself. For instance:

Darrin Altman wrote:I would count how many numbers you read off and then divide by that number. Contemplating this I am trying to think of a method to figure out how many numbers the user input but am at a loss. I am going to go back and read the chapter again and see if I can figure this out.

So how do you count how many numbers are added? each time you hear a new value from me, you increase your count by 1.

So you need to keep track of two things...the running total of all the input numbers, and a count of how many numbers you've gotten.

Darrin Altman wrote:I would count how many numbers you read off and then divide by that number. Contemplating this I am trying to think of a method to figure out how many numbers the user input but am at a loss. I am going to go back and read the chapter again and see if I can figure this out.

So how do you count how many numbers are added? each time you hear a new value from me, you increase your count by 1.

So you need to keep track of two things...the running total of all the input numbers, and a count of how many numbers you've gotten.

I really don't believe up to this point the book has shown how to set up an input counter. I will continue to work on this and report back.

there is nothing fancy about it...it would just be a variable you create. just like you have your "double average = 0", you would set up a "int numInputs = 0".

Then each time you get a new value input, you add 1 to it. there are several ways of doing that:

numInputs = numInputs + 1;

or

numInputs += 1;

in fact, adding 1 to an int is so common, there is a special shortcut:

fred rosenberger wrote:there is nothing fancy about it...it would just be a variable you create. just like you have your "double average = 0", you would set up a "int numInputs = 0".

Then each time you get a new value input, you add 1 to it. there are several ways of doing that:

numInputs = numInputs + 1;

or

numInputs += 1;

in fact, adding 1 to an int is so common, there is a special shortcut:

numInputs++;

As soon as I get home from work I will work with this and report back. Thank you very much for your patience and guidance!

I completed the exercise. Thank you to everyone who gave input, especially Fred. I added a couple of lines at the end just to verify the program was running correctly. I realized I had to put the counter after break because if not it would count the input of the sentinel. I'm sure this could be refined and improved but it works. Thank you all again.

I don't like the break. Try thisYes, you do need the second pair of () around the = operator in the line beginning while, otherwise you get the operator precedence wrong.

Campbell's post illustrates the difference in coding style. Personally, I find this line too complicated:

while((score = readInt("Enter next score, or " + SENTINEL + " to exit: ")) != SENTINEL)

IMHO, there is too much going on there - you're building a string, getting user input, saving it comparing it to another value and controlling a while loop.

Whatever happened to "one line of code should do one thing"?

It's certainly not WRONG - it's just not a line I would ever write.

Again, there is nothing wrong with this. but personally, I would not calculate the average on each iteration. there's no need for that. I'd move that line outside the loop, and only calculate it once.

It won't make much difference here, but if you were doing some INTENSIVE computation that took a long time, why waste the CPU cycles? it does nothing but slow down your program. Further, at some point, someone else will come along and read your code (possibly yourself) and wonder why you keep re-calculating the average on each loop.