# Program should display number of positives\negatives\total\average

• October 7th, 2012, 03:42 PM
alias22
Program should display number of positives\negatives\total\average
Should display like so:

Quote:

Enter an int value, the program exits if the input is 0:
1 2 -1 3 0
The number of positives is 3
The number of negatives is 1
The total is 5
The average is 1.25

Code java:

```  import java.util.Scanner;   public class main41 {   public static void main(String[] args) { System.out.println("Enter an int value, the program exits if the input is 0\n\n");     int total=0, numOfPos=0, numOfNeg=0, n=0; double average=0;   Scanner sin = new Scanner(System.in); n = sin.nextInt();     total = total+n; average = total/n;   while(n!=0) { //if the number is less than zero, count it if(n<0) { numOfNeg++; } //if the number is greater than zero, count it if(n>0) { numOfPos++; } System.out.print("\r\nThe number of positives is: " + numOfPos); System.out.print("\r\nThe number of negatives is " + numOfNeg); System.out.print("\r\n\nThe total is: " + total); System.out.print("\r\nThe average is: "+ average); } }     }```
• October 7th, 2012, 05:38 PM
Norm
Re: Program should display number of positives\negatives\total\average
Quote:

my code is NOT working,
Please explain what "NOT working" means.
Does the program print out too many times, instead of printing one time at the end?
• October 7th, 2012, 05:48 PM
alias22
Re: Program should display number of positives\negatives\total\average
Quote:

Originally Posted by Norm
Please explain what "NOT working" means.
Does the program print out too many times, instead of printing one time at the end?

Yes.. and i've tried doing many different things with the while loop, even taking it out/changing it to an if statement, but it won't work for me? I don't know what to do to fix it
(there are no errors showing in eclipse btw)
• October 7th, 2012, 06:01 PM
Norm
Re: Program should display number of positives\negatives\total\average
Quote:

it won't work for me
What does the program do? You have not explained what "won't work" means.
What do you want to change about the way it works?

Try playing computer with the code. Look at the order that the statements are executed and write down the values of the variables as they are changed and look at the current value of the variable when it is tested and used.
• October 7th, 2012, 06:47 PM
sparky971
Re: Program should display number of positives\negatives\total\average
try putting the System.outs outside the while(n!=0 ) because otherwise each time n is not = 0 it will print it out :)
• October 7th, 2012, 07:03 PM
djpista
Re: Program should display number of positives\negatives\total\average
Code :

```while(n!=0) { //if the number is less than zero, count it if(n<0) { numOfNeg++; } //if the number is greater than zero, count it if(n>0) { numOfPos++; }   } System.out.print("\r\nThe number of positives is: " + numOfPos); System.out.print("\r\nThe number of negatives is " + numOfNeg); System.out.print("\r\n\nThe total is: " + total); System.out.print("\r\nThe average is: "+ average);```
just do it like this and just one time will print
• October 7th, 2012, 09:15 PM
Zaphod_b
Re: Program should display number of positives\negatives\total\average

1. Prompt the user for a non-zero integer, read the value of n
2. Do a little bookkeeping and execute the following loop
WHILE n is not equal to zero
LOOP
Test value and increment either numOfNeg or numOfPos
Do some more bookkeeping and print out a bunch of stuff
END LOOP

Now, what's missing here? I mean, why does it just print and print (and print)?

Well...
It just goes on and on (and on and on) since nothing in the whole program changes the value of n after that first user input, right? How the heck could it ever get out of that loop, which depends on the value of n?

Cheers!

Z
• October 8th, 2012, 04:00 PM
alias22
Re: Program should display number of positives\negatives\total\average
i tried again, and it doesn't work

Code java:

```import java.util.Scanner;   public class main41 {   public static void main(String[] args) { System.out.println("Enter an int value, the program exits if the input is 0\n\n");     int total=0, numOfPos=0, numOfNeg=0; double average=0;   Scanner sin = new Scanner(System.in); int n = sin.nextInt();   while(n!=0) { //if the number is less than zero, count it if(n<0) { numOfNeg++; } //if the number is greater than zero, count it if(n>0) { numOfPos++; } }   total = total+n; average = total/n;   System.out.print("\r\n\nThe total is: " + total); System.out.print("\r\nThe average is: "+ average); System.out.print("\r\nThe number of negatives is " + numOfNeg); System.out.print("\r\nThe number of positives is: " + numOfPos);   } }```

as you can see i took the system.out.prints out of the while loop, but now what the program is doing is, it is just taking numbers from the user forever, it never stops after I hit enter... i want it so that after i input a few numbers and hit enter, it should go and print out the results. This program should be fairly simple to do and I don't know why i can't get it going
• October 8th, 2012, 04:13 PM
Norm
Re: Program should display number of positives\negatives\total\average
The while loop is controlled by the value of n. Once inside the loop the only way to exit the loop is to change the value of n.
Look at the code in the loop. Is the value of n changed inside the loop? It must be changed for the execution to exit the loop.

Try working on the steps the code is supposed to do before writing any more code. Make a list of the steps the code should do (pseudo code) before writing any more code.
An example for adding the first numbers and printing the count:
Set counter to 0
begin loop using index (for loop)
end loop
print counter
• October 21st, 2012, 12:13 PM
alias22
Re: Program should display number of positives\negatives\total\average
hello again.. I found that this program is a lot more difficult to code than what it seems... I have it finally working now although it's buggy and if i enter -1 i get no output...

Code java:

```import java.util.Scanner;   public class main41 {   public static void main(String[] args) { System.out.print("Enter an int value, the program exits if the input is 0");   System.out.println("\n");   Scanner sin = new Scanner(System.in);   int total=0, numOfPos=0, numOfNeg=0; double average = 0.0, numbers = 0.0; final int maxLength = 6;     int n = 1;     while (n!=0 && n<maxLength) {   n++;     //if the number is less than zero, count it if(n<0) { numOfNeg++; if(numbers<=numOfNeg) { numbers++; } }   //if the number is greater than zero, count it else if(n>0) { numOfPos++; if(numbers<=numOfPos) { numbers++; } }   else { System.exit(0); }     n = sin.nextInt(); total = total + n; }   average = total/numbers;   if (n == 0) { System.out.println("\n\nERROR"); }   else if(total>0 || total<0 && n<maxLength) { System.out.print("\nThe number of positives is: " + numOfPos); System.out.print("\nThe number of negatives is: " + numOfNeg); System.out.print("\nThe total is: " + total); System.out.print("\nThe average is: "+ average); } } }```

some outputs:
Quote:

Enter an int value, the program exits if the input is 0

1 2 3 4 5 6

The number of positives is: 6
The number of negatives is: 0
The total is: 21
The average is: 3.5
Quote:

Enter an int value, the program exits if the input is 0

-2 3 4 5 7

The number of positives is: 4
The number of negatives is: 1
The total is: 17
The average is: 3.4
bugs:
Quote:

Enter an int value, the program exits if the input is 0

1 4 6 7 8

The number of positives is: 3
The number of negatives is: 0
The total is: 11
The average is: 3.6666666666666665
Quote:

Enter an int value, the program exits if the input is 0

-1 2 4 6 7
[terminates after enter (i think because n is initially declared as 1 and 1-1=0)]
believe me, you guys giving me tips like that really does not help me at all.. i've tried your advices, and tested 100's of other ways and they do not **** work. The code above was the only way i could get this 'close' to working.. the only way I can really see where i'm going wrong is if someone actually writes a 100% working code to this program. Otherwise I will not learn.
• October 21st, 2012, 02:34 PM
Norm
Re: Program should display number of positives\negatives\total\average
Try debugging the code by adding some println statements that print out the value of n each time its value is changed and each time its value is tested. You need to see what the code you have written is doing so you can understand why the program is doing what it is doing.

Some things to consider and document in your code:
what is the variable n used for? Add a comment describing what values it is supposed to contain.
What is the variable maxLength used for? How does it relate to n?

Why read data from the user into the variable n? Does that destroy what it is used for?
• October 21st, 2012, 03:21 PM
Zaphod_b
Re: Program should display number of positives\negatives\total\average
Quote:

Originally Posted by alias22
... the only way I can really see where i'm going wrong is if someone actually writes a 100% working code...

I'll go half-way. I will write 100% working pseudo-code.

There are a number of ways to organize the program, and you don't have to do it the way that I will illustrate, but unless you see a way more suitable to your capabilities, I suggest that you try the following:

(You have already done the first couple of things, but I include them here for completeness.)

Code :

```Declare Scanner object for System.in   Declare variables for numbers, total, numPos, numNeg. Initialize all to values of zero.   Prompt for a non-zero integer Declare an int variable named n and set its value from the Scanner object   Repeat the following loop as long as n is not equal to zero: BEGIN LOOP   Increment the numbers variable.   Add the value of n to the total variable.   IF n IS GREATER THAN ZERO THEN INCREMENT numPos ELSE IF n IS LESS THAN ZERO THEN INCREMENT numNeg END IF   Prompt for a non-zero integer Set n to the value read by the Scanner object   END LOOP   Print out numPos, numNeg, total If numbers IS GREATER THAN ZERO THEN Calculate and print average = total/numbers END IF```

If there is any part of it that you don't understand or don't see how to make Java out of it then ask specific questions.

Cheers!

Z