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.


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

Results 1 to 4 of 4

Thread: Problem with division

  1. #1
    Junior Member
    Join Date
    Jul 2014
    Thanked 0 Times in 0 Posts

    Exclamation Problem with division

    import java.util.Scanner;

    public class CountPosAndNegNumbers {

    public static void main(String[] args) {

    Scanner input = new Scanner(System.in);

    int positive = 0;
    int negative = 0;
    int averagePos = 0;
    int averageNeg= 0;

    System.out.println("Enter an integer, the input ends if it is 0:");
    int numbers = input.nextInt();

    do {

    if (numbers > 0)

    positive += numbers;

    else if (numbers < 0)

    negative += numbers;

    numbers = input.nextInt();
    averagePos = Math.round(positive);
    averageNeg = Math.round(negative);

    } while (numbers != 0);

    System.out.println("The number of positives is:" + positive);
    System.out.println("The number of negatives is:" + negative);
    System.out.println("Average of positive numbers:" + averagePos);
    System.out.println("Average of negative numbers:" + averageNeg);


    I need help to find the average of the integers that are input but can't manage to solve the problem.

  2. #2
    Junior Member
    Join Date
    Jul 2014
    My Mood
    Thanked 0 Times in 0 Posts

    Wink Re: Problem with division

    you can do it easily without calling Math.round()

  3. #3
    Senior Member Ada Lovelace's Avatar
    Join Date
    May 2014
    South England UK
    My Mood
    Thanked 72 Times in 66 Posts

    Default Re: Problem with division

    If you just want the whole integer (no decimal) then you
    need to count the total amount of inputs for each type -
    i.e total input of positive, total input of negative. You would
    then use these values in the calculation against the totals of
    each numeric value result (total of negative numbers etc).

    Wishes Ada xx
    If to Err is human - then programmers are most human of us all.
    "The Analytical Engine offers a new, a vast, and a powerful language . . .
    for the purposes of mankind
    Augusta Ada Byron, Lady Lovelace (1851)

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    So. Maryland, USA
    My Mood
    Thanked 894 Times in 877 Posts

    Default Re: Problem with division

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.

    Please post your code correctly using code or highlight tags which are explained near the top of the above link.

Similar Threads

  1. division
    By ericgomez in forum What's Wrong With My Code?
    Replies: 7
    Last Post: May 23rd, 2013, 10:15 AM
  2. Trial Division Loop Problem
    By Wwong3333 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: October 8th, 2012, 08:17 AM
  3. Division by Zero
    By mael331 in forum Java Theory & Questions
    Replies: 16
    Last Post: December 6th, 2011, 05:13 PM
  4. division problem
    By vgenopoulos in forum Java Theory & Questions
    Replies: 1
    Last Post: July 30th, 2010, 01:51 PM
  5. [SOLVED] Java program to write a code to detect divisibility of any number by 11
    By Java'88 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: August 11th, 2009, 10:41 PM