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: Average and counting problem (sorta)

  1. #1
    Member
    Join Date
    Feb 2014
    Posts
    58
    Thanks
    25
    Thanked 0 Times in 0 Posts

    Default Average and counting problem (sorta)

    So, my code compiles fine and everything with a do-while loop, but when it counts how many numbers there are and averages them all, the count adds the 0 and the average also includes the 0 which I don't want it to. Please help and thank you in advance!

    import java.util.Scanner;
     
    //Counts pos #, neg #, and averages them.
    public class Avg_Pos_Neg_Num {
    	public static void main(String[] args) {
     
    //what integers?
    		int numbers = 0;
    		int total = 0;
    		int positive = 0;
    		int negative = 0;
    		int sum = 0;
    		double avg = 0.0;
     
    		Scanner input = new Scanner(System.in);
     
    //Tell user to enter pos and neg numbers with spaces.
    //while loop
    		do { 
    			System.out.print("Enter any positive or negative number (0 ends program): ");
    			numbers = input.nextInt();
     
    			total++;
    			if (numbers > 0) {positive++;}
    			else if (numbers < 0) {negative++;}
    		}
    		while (numbers != 0);
     
    //Compute
    		sum = (positive + negative);
    		avg = ((double)sum / total);
     
    //Output
    		System.out.println("The number of positive numbers is " + positive);
    		System.out.println("The number of negative numbers is " + negative);
    		System.out.println("There are " + total + " numbers");
    		System.out.println("The average of the numbers is " + avg);
     
    			}
    		}


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Average and counting problem (sorta)

    One way would be to add code to test for the 0 with an if statement and not do the add.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Feb 2014
    Posts
    58
    Thanks
    25
    Thanked 0 Times in 0 Posts

    Default Re: Average and counting problem (sorta)

    What do you mean to add code to test for the 0 with an if statement?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Average and counting problem (sorta)

    if(value is not 0)
    count it
    If you don't understand my answer, don't ignore it, ask a question.

  5. The Following User Says Thank You to Norm For This Useful Post:

    Elyril (February 18th, 2014)

  6. #5
    Member
    Join Date
    Feb 2014
    Posts
    58
    Thanks
    25
    Thanked 0 Times in 0 Posts

    Default Re: Average and counting problem (sorta)

    Thanks for that. I just added
     else if (numbers == 0) {total--;}
    into the nested if's and it counted right, but now, I found out my averages, even if I go put a negative into the equation, the average won't go negative and just stay positive. I also put
     int negative = -0;
    if that helps.

  7. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Average and counting problem (sorta)

    Can you give an example run to show what you mean? Copy the run from the console output and post it. Include a description of how the run is different than you expect.

  8. #7
    Member
    Join Date
    Feb 2014
    Posts
    58
    Thanks
    25
    Thanked 0 Times in 0 Posts

    Default Re: Average and counting problem (sorta)

    Enter any positive or negative number (0 ends program): -5
    Enter any positive or negative number (0 ends program): 2
    Enter any positive or negative number (0 ends program): -10
    Enter any positive or negative number (0 ends program): 3
    Enter any positive or negative number (0 ends program): 0
    The number of positive numbers is 2
    The number of negative numbers is 2
    There are 4 numbers
    The average of the numbers is 1.0

    So, I guess the program really isn't doing the average right either. Every time I compile the program, it counts all the numbers correctly, but it won't average the numbers because in this result, I should be getting -2.5 as the average, but it's only 1.0 and doesn't turn into a negative.

  9. #8
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Average and counting problem (sorta)

    Check your formula for 'sum.' What is it currently the sum of? Is that 'sum' going to give you the desired average?

    Hint: The formula for 'sum' (whatever it is) should be inside the do/while loop.

  10. #9
    Member
    Join Date
    Feb 2014
    Posts
    58
    Thanks
    25
    Thanked 0 Times in 0 Posts

    Default Re: Average and counting problem (sorta)

    I changed my loop and compute to this
    //do-while loop w/ sum inside
    		do { 
    			System.out.print("Enter any positive or negative number (0 ends program): ");
    			numbers = input.nextInt();
     
    			total++;
    			if (numbers > 0) {positive++;}
    			else if (numbers < 0) {negative++;}
    			else if (numbers == 0) {total--;}
    			sum = (numbers);
    		}
    		while (numbers != 0);
     
    //Compute 
    		avg = (sum / total);
    but now it turns out to 0.0 for the average. I think it might be because of the 0, but when I tried to output the 'sum' as well, that also came out to 0.0 too.

  11. #10
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Average and counting problem (sorta)

    'sum' needs to sum all numbers entered by the user, not just the last number entered, which is what your code currently does. That's why it's zero.

    In words, each time through the loop, sum = previousSum + numberEntered

    What is previousSum?

  12. The Following User Says Thank You to GregBrannon For This Useful Post:

    Elyril (February 19th, 2014)

  13. #11
    Member
    Join Date
    Dec 2013
    Posts
    40
    My Mood
    Amazed
    Thanks
    2
    Thanked 11 Times in 11 Posts

    Default Re: Average and counting problem (sorta)

    Recommendation:
    1. Change the line:
     numbers = input.nextInt();
    to
     numbers += input.nextInt();

    2. Replace:
     sum = (positive + negative);
    with:
     sum = numbers;
    Who holds the KEY to all knowledge?

  14. The Following User Says Thank You to Mugambbo For This Useful Post:

    Elyril (February 19th, 2014)

  15. #12
    Member
    Join Date
    Feb 2014
    Posts
    58
    Thanks
    25
    Thanked 0 Times in 0 Posts

    Default Re: Average and counting problem (sorta)

    Thanks. I did this instead:
    sum += numbers
    and
    avg = ((double) sum / total)

Similar Threads

  1. Array counting problem
    By Variumzky in forum What's Wrong With My Code?
    Replies: 8
    Last Post: August 2nd, 2013, 12:24 PM
  2. Counting Days problem with object
    By hawkeye10 in forum Object Oriented Programming
    Replies: 3
    Last Post: September 6th, 2012, 09:48 AM
  3. Help with Average and Variables Please =)
    By Raymond Pittman in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 9th, 2011, 10:02 AM
  4. Moving Average
    By Donieob in forum What's Wrong With My Code?
    Replies: 0
    Last Post: September 3rd, 2011, 08:58 AM
  5. Moving average
    By bondage in forum Loops & Control Statements
    Replies: 0
    Last Post: May 7th, 2010, 04:20 AM

Tags for this Thread