Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 12 of 12

Thread: Program should display number of positives\negatives\total\average

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    Should display like so:

    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
    hi, my code is NOT working, I don't understand why, please help

     
    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);
    	}
    }
     
     
    }
    Last edited by alias22; October 7th, 2012 at 03:44 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Program should display number of positives\negatives\total\average

    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?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Oct 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Program should display number of positives\negatives\total\average

    Quote Originally Posted by Norm View Post
    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)

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Program should display number of positives\negatives\total\average

    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.
    Last edited by Norm; October 7th, 2012 at 06:03 PM.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Sep 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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

  6. #6
    Junior Member
    Join Date
    Oct 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Program should display number of positives\negatives\total\average

    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

  7. #7
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Program should display number of positives\negatives\total\average

    Let's see if I can follow your code, alias22:

    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

  8. #8
    Junior Member
    Join Date
    Oct 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Program should display number of positives\negatives\total\average

    i tried again, and it doesn't work

    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

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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)
    add loop index to counter
    end loop
    print counter
    Last edited by Norm; October 8th, 2012 at 04:18 PM.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Junior Member
    Join Date
    Oct 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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...

    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:
    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
    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:
    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
    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.
    Last edited by Norm; October 21st, 2012 at 02:25 PM. Reason: Removed obscenity

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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?
    Last edited by Norm; October 21st, 2012 at 02:46 PM.
    If you don't understand my answer, don't ignore it, ask a question.

  12. #12
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Program should display number of positives\negatives\total\average

    Quote Originally Posted by alias22 View Post
    ... 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.)


    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

Similar Threads

  1. [SOLVED] (Total begginner) simple GUI program nt displayin
    By moneyman021 in forum What's Wrong With My Code?
    Replies: 11
    Last Post: January 10th, 2012, 06:26 PM
  2. Help with JFrame program that calculates average
    By ePerKar3 in forum AWT / Java Swing
    Replies: 3
    Last Post: November 4th, 2011, 08:48 AM
  3. program wont renew total
    By that_guy in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 17th, 2011, 01:00 PM
  4. Find total number less than average
    By maximus20895 in forum Collections and Generics
    Replies: 2
    Last Post: December 1st, 2010, 01:46 PM
  5. Average program with array and loop and JOptionPane.
    By jeremykatz in forum AWT / Java Swing
    Replies: 6
    Last Post: October 25th, 2009, 02:33 PM