# Thread: Help with while iterative procedure

1. Member
Join Date
Oct 2010
Posts
13
Rep Power
0

## Help with while iterative procedure

Hey there,

I could use some help with my homework once again.

Here's the question:

Write a Java program which uses a while iterative procedure to allow the user to enter a list of numbers terminated by 0, and which totals them, finds their average and displays their total and average.

I'm not completely sure how to write the code to produce both sum and average, but I googled and researched and gave it a shot and... well, my code definitely is not right. If anyone can explain how to write this and maybe let me know where I'm going wrong, I'd really appreciate it.

import java.util.Scanner;
public class SwitchStatement {
public static void main(String[] args) {
Scanner inp = new Scanner (System.in);
int x = 1;
int sum = 0;
System.out.println ("Please enter a sequence of integer numbers ending with 0");
while (x != 0) {
x = inp.nextInt();
sum +=x;
}
System.out.println ( "Sum of integers is " + sum);

int count; // The number of positive integers.

count = 0;
System.out.println(x);
while (x != 0) {
count++; // Count the input by adding 1 to count.
x = ((int)sum) / count;
System.out.println ( "Average of integers is " +x);
}
}
}

When I run the prorgram, I can enter the integers, and get the sum, but I can't get the average to display.
Here's what it looks like:

Please enter a sequence of integer numbers ending with 0
21
46
0

Sum of integers is 67
0

Any help/explanation would be really appreciated. Thank you!

2. Moderator
Join Date
Apr 2009
Posts
11,302
Rep Power
18
You should be counting the number of numbers while they're being read in, not afterwards.

3. Also, your variable 'x' is never reset. At the end of your first while loop, 'x' will be equal to zero (which is what is printed out) and your second while loop is skipped entirely because of this.

4. Moderator
Join Date
Apr 2009
Posts
11,302
Rep Power
18
Oh, and use CODE tags, because I completely missed that bit!
:)

5. Member
Join Date
Oct 2010
Posts
13
Rep Power
0
Thanks for responding!

I'm still not getting it. I'm fairly new to Java so hopefully I don't sound completely stupid :)

I've edited the code but it still isn't running properly. I understood moving the count piece of the code and did that. I'm not quite sure how to reset my 'x' variable. I gave it a shot, but it didn't seem to do the trick so maybe I didn't quite understand.

Sorry about the code tags. I didn't know what that was but I figured it out.

Java Code:
```import java.util.Scanner;
public class SwitchStatement{
public static void main (String[] args){
Scanner mg = new Scanner (System.in);
int x = 1;
int sum = 0;
System.out.println ("Please enter a sequence of integer numbers ending with 0");
while (x != 0){
x = mg.nextInt();
sum +=x;
}
System.out.println ("Sum of integers is " +sum);

int count;
count = 0;
count++;
int avg = (x/count);

while (x != 0){
x = mg.nextInt();

}
System.out.println("Average of integers is " +avg);
}
}```
When I run it, I get this:

Please enter a sequence of integer numbers ending with 0
72
84
0

Sum of integers is 156
Average of integers is 0
Last edited by SweetLD215; 10-13-2010 at 12:26 AM.

6. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,565
Rep Power
12
What is count supposed to represent?

Say I entered three nonzero numbers, what value do you expect count to have? Where, in your code, do you intend that it gets that value?

7. Member
Join Date
Oct 2010
Posts
13
Rep Power
0
pbrockway2 thank you! Your voice was in my head as I kept playing with the code. It was when you asked what value I wanted count to have and where the code was supposed to find it. I kept thinking "three, but I have no idea" so finally... I was like... "ok, I'm stupid. x is where it gets it" So... a bit of trial and error and I think I got it.

I nearly did a EUREAKA dance! LOL

Even though it runs correctly if someone wouldn't mind confirming that it looks good, that would be wonderful!

Java Code:
``` import java.util.Scanner;
public class SwitchStatement{
static Scanner mg = new Scanner (System.in);

public static void main (String[] args){

int x = 1;
int sum = 0;
int avg = 0;
int count=x;
count++;

System.out.println ("Please enter a sequence of integer numbers ending with 0");
while (x != 0){
x = mg.nextInt();
sum +=x;
avg = (sum/count);

}
System.out.println ("Sum of integers is " +sum);
System.out.println("Average of integers is " +avg);
}
}```

8. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,565
Rep Power
12
I agree in that code a count of 3 would be useful. But wouldn't count end up as two in the code you posted?

Java Code:
```int x = 1;
int sum = 0;
int avg = 0;
int count=x;  // count is now 1
count++;      // count is now 2
// count is never changed from this point on```
The problem is that the user enters all these numbers, but count never changes. That can't be right: count should increment (get bigger by 1) for every nonzero number that is entered.

9. Member
Join Date
Oct 2010
Posts
13
Rep Power
0
Ahhhh ok, that totally makes sense. Hmm, I'll have to research to figure out how to make the count keep going. I'm not really sure how to do that one.
Last edited by SweetLD215; 10-13-2010 at 02:15 AM.

10. Member
Join Date
Oct 2010
Posts
13
Rep Power
0
Ok so I'm thinking that maybe putting the count++; in the loop and taking the sum and average part out of it would help but now that creates some other issue and it won't sum or average anything.

I'm feeling kind of stupid right now being unable to figure this out.

11. Member
Join Date
Oct 2010
Posts
13
Rep Power
0
I think I got it!

I hope... I got it.

Java Code:
``` import java.util.Scanner;
public class SwitchStatement{
static Scanner mg = new Scanner (System.in);

public static void main (String[] args){

int x = 1;
int sum = 0;
int avg = 0;
int count = 0;

System.out.println ("Please enter a sequence of integer numbers ending with 0");
while (x != 0){
x = mg.nextInt();
sum +=x;
count++;
}

count--;
avg = sum/count;

System.out.println ("Sum of integers is " +sum);
System.out.println("Average of integers is " +avg);
}
}```

12. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,565
Rep Power
12
Java Code:
```sum +=x;
count++;```

This in the loop looks good to me.

I'm not sure about that count-- after the loop though. Why do you do that? Also you could put an extra line of output at the end just to check things. (Although if this an assignment you might take it out later)

Java Code:
```System.out.println ("Number of integers entered was " + count);
System.out.println ("Sum of integers is " +sum);
System.out.println("Average of integers is " +avg);```

Then try with lots of input. At this point you have a reasonable looking piece of code and you should be trying to break it: to make sure there is nothing wrong with it.

13. Member
Join Date
Oct 2010
Posts
13
Rep Power
0
Yes, it is for an assignment.

I put the count--; in because it kept counting the zero at the end as part of the sequence when averaging the numbers. I wanted to somehow make it not count the zero, and this was all I could come up with.
Is this not a good way to do that?

I just entered in the piece of code you gave me and tried using a string of 9 numbers then a string of 14 numbers and both seem to work. =)

14. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,565
Rep Power
12
OK, I hadn't actually run the code at any point. And I can see you're right: the zero gets "counted" so you have to take one off to allow for that. Well done!

15. Member
Join Date
Oct 2010
Posts
13
Rep Power
0
Thank you! It was a lot of searching to figure that one out.

Now I'm aiming for some extra credit assignments and hopefully not pull my hair out.

I really appreciate your help. You've been amazing!

16. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,565
Rep Power
12
You're welcome.

17. Glad you got it figured out, sorry I would have helped more but I was away from my computer all week. Gratz.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•