Help with while iterative procedure

• 10-12-2010, 01:55 AM
SweetLD215
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!
• 10-12-2010, 09:21 AM
Tolls
You should be counting the number of numbers while they're being read in, not afterwards.
• 10-12-2010, 10:50 AM
joshdgreen
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.
• 10-12-2010, 10:54 AM
Tolls
Oh, and use CODE tags, because I completely missed that bit!
:)
• 10-12-2010, 11:21 PM
SweetLD215
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.

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
• 10-13-2010, 12:22 AM
pbrockway2
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?
• 10-13-2010, 01:01 AM
SweetLD215
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!

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); } }```
• 10-13-2010, 01:08 AM
pbrockway2
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?

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.
• 10-13-2010, 01:09 AM
SweetLD215
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.
• 10-13-2010, 01:42 AM
SweetLD215
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.
• 10-13-2010, 02:34 AM
SweetLD215
I think I got it!

I hope... I got it.

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); } }```
• 10-13-2010, 03:41 AM
pbrockway2
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)

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.
• 10-13-2010, 05:03 AM
SweetLD215
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. =)
• 10-13-2010, 07:22 AM
pbrockway2
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!
• 10-13-2010, 07:43 AM
SweetLD215
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!
• 10-13-2010, 08:08 PM
pbrockway2
You're welcome.
• 10-20-2010, 06:54 AM
joshdgreen
Glad you got it figured out, sorry I would have helped more but I was away from my computer all week. Gratz.