Results 1 to 14 of 14

Math Help - Java Program - Summing Integers

  1. #1
    Junior Member
    Joined
    Jul 2006
    Posts
    45

    Java Program - Summing Integers

    Hi guys--I have to write a program that reads a set of integers and then finds and prints the sum of the even and odd integers.

    For instance, if the numbers were: 9, 10, 17, -20, 22, -3
    evens should be 12
    odds 23
    all 35

    I'm messing up the math but here's what I have below--help!!!

    import java.util.*;

    public class Integers {

    static Scanner console = new Scanner(System.in);

    static final int SENTINEL = -999;

    public static void main(String[] args) {

    int number;
    int sumAll = 0;
    int sumEven = 0;
    int sumOdd = 0;

    System.out.println("Enter integers, positive, negative, or zeros ending with " + SENTINEL);
    number = console.nextInt();
    System.out.println();

    int count = 0;
    while (number != SENTINEL) {
    sumAll += count;
    number = console.nextInt();

    if (count % 2 == 0)
    sumEven += count;
    else
    sumOdd += count;

    count++;
    }

    System.out.println("Sum of all integers: " + sumAll);
    System.out.println("Sum of the even integers: " + sumEven);
    System.out.println("Sum of the odd integers: " + sumOdd);

    }
    }
    Follow Math Help Forum on Facebook and Google+

  2. #2
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by harold View Post
    Hi guys--I have to write a program that reads a set of integers and then finds and prints the sum of the even and odd integers.

    For instance, if the numbers were: 9, 10, 17, -20, 22, -3
    evens should be 12
    odds 23
    all 35

    I'm messing up the math but here's what I have below--help!!!

    import java.util.*;

    public class Integers {

    static Scanner console = new Scanner(System.in);

    static final int SENTINEL = -999;

    public static void main(String[] args) {

    int number;
    int sumAll = 0;
    int sumEven = 0;
    int sumOdd = 0;

    System.out.println("Enter integers, positive, negative, or zeros ending with " + SENTINEL);
    number = console.nextInt();
    System.out.println();

    int count = 0;
    while (number != SENTINEL) {
    sumAll += count;
    sumAll += number;

    number = console.nextInt();

    if (count % 2 == 0)
    sumEven += count;
    else
    sumOdd += count;
    if (number % 2 == 0)
    sumEven += number;
    else
    sumOdd += number;


    count++;
    }

    System.out.println("Sum of all integers: " + sumAll);
    System.out.println("Sum of the even integers: " + sumEven);
    System.out.println("Sum of the odd integers: " + sumOdd);

    }
    }
    RonL
    Follow Math Help Forum on Facebook and Google+

  3. #3
    Junior Member
    Joined
    Jul 2006
    Posts
    45
    Hi Captain,

    I applied what you said but the evens and odds still aren't right.

    For the above input, I get the following output--all integers: 35
    even integers: -13
    odd integers: -960

    -999 is the sentinel--it's supposed to stop reading the input when it gets to that number...not sure what's happening...
    Follow Math Help Forum on Facebook and Google+

  4. #4
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by harold View Post
    Hi Captain,

    I applied what you said but the evens and odds still aren't right.

    For the above input, I get the following output--all integers: 35
    even integers: -13
    odd integers: -960

    -999 is the sentinel--it's supposed to stop reading the input when it gets to that number...not sure what's happening...
    You need to read the first number before entering the loop, and inside the
    loop the reading of the next number should be done at the end.

    RonL
    Follow Math Help Forum on Facebook and Google+

  5. #5
    Junior Member
    Joined
    Jul 2006
    Posts
    45
    Tried that CB...the output is still wrong...I get:

    Sum of all integers: 35
    Sum of the even integers: 48
    Sum of the odd integers: -13
    Follow Math Help Forum on Facebook and Google+

  6. #6
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by harold View Post
    Tried that CB...the output is still wrong...I get:

    Sum of all integers: 35
    Sum of the even integers: 48
    Sum of the odd integers: -13
    Can you post the code that you are now using

    RonL
    Follow Math Help Forum on Facebook and Google+

  7. #7
    Junior Member
    Joined
    Jul 2006
    Posts
    45
    import java.util.*;

    public class Integers {

    static Scanner console = new Scanner(System.in);
    static final int SENTINEL = -999;

    public static void main(String[] args) {

    int number;
    int sumAll = 0;
    int sumEven = 0;
    int sumOdd = 0;

    System.out.println("Enter integers, positive, negative, or zeros ending with " + SENTINEL);
    number = console.nextInt();
    System.out.println();

    while (number != SENTINEL) {
    sumAll += number;

    if (number % 2 == 0)
    sumEven += number;
    else
    sumOdd += number;

    number = console.nextInt();
    }

    System.out.println("Sum of all integers: " + sumAll);
    System.out.println("Sum of the even integers: " + sumEven);
    System.out.println("Sum of the odd integers: " + sumOdd);
    }
    }
    Follow Math Help Forum on Facebook and Google+

  8. #8
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by harold View Post
    import java.util.*;

    public class Integers {

    static Scanner console = new Scanner(System.in);
    static final int SENTINEL = -999;

    public static void main(String[] args) {

    int number;
    int sumAll = 0;
    int sumEven = 0;
    int sumOdd = 0;

    System.out.println("Enter integers, positive, negative, or zeros ending with " + SENTINEL);
    number = console.nextInt();
    System.out.println();

    while (number != SENTINEL) {
    sumAll += number;

    if (number % 2 == 0)
    sumEven += number;
    else
    sumOdd += number;

    number = console.nextInt();
    }

    System.out.println("Sum of all integers: " + sumAll);
    System.out.println("Sum of the even integers: " + sumEven);
    System.out.println("Sum of the odd integers: " + sumOdd);
    }
    }
    This looks good to me.

    The fact that it seems to get sumAll correct indicates that probably
    the loop control and reading the data is all OK.

    You probably need to run this in a debugger to see what exactly is
    happening.

    The most likely suspect is the code fragment:

    Code:
      if (number % 2 == 0)
        sumEven += number;
      else
        sumOdd += number;
    There are a number of ways this may not be doing what we expect,
    it could be an idiosyncracy of the % operator, or the form of the
    if-else statement. The debugger ought to identify such problems.

    Alternativly you could try the following (not nice but could be interesting)

    Code:
      if ( ((number*number) % 2) == 0)
        {sumEven += number;}
      else
        {sumOdd += number;}
    RonL
    Follow Math Help Forum on Facebook and Google+

  9. #9
    Senior Member
    Joined
    Apr 2006
    Posts
    399
    Awards
    1
    Quote Originally Posted by CaptainBlack View Post
    This looks good to me.

    The fact that it seems to get sumAll correct indicates that probably
    the loop control and reading the data is all OK.

    You probably need to run this in a debugger to see what exactly is
    happening.

    The most likely suspect is the code fragment:

    Code:
      if (number % 2 == 0)
        sumEven += number;
      else
        sumOdd += number;
    There are a number of ways this may not be doing what we expect,
    it could be an idiosyncracy of the % operator, or the form of the
    if-else statement. The debugger ought to identify such problems.

    Alternativly you could try the following (not nice but could be interesting)

    Code:
      if ( ((number*number) % 2) == 0)
        {sumEven += number;}
      else
        {sumOdd += number;}
    RonL
    When you just cannot figure out what is going wrong and you don't have a debugger, you can always put extra print statements in suspect code to help you. For example, this will tell you if the mod operator is doing anything unexpected.

    Code:
      if (number % 2 == 0)
        System.out.println("Even: "+number);
      else
        System.out.println("Odd: "+number);
    Follow Math Help Forum on Facebook and Google+

  10. #10
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by JakeD View Post
    When you just cannot figure out what is going wrong and you don't have a debugger, you can always put extra print statements in suspect code to help you. For example, this will tell you if the mod operator is doing anything unexpected.

    Code:
      if (number % 2 == 0)
        System.out.println("Even: "+number);
      else
        System.out.println("Odd: "+number);
    Excuse me, I will just put this clock back 20 years

    *%£$**£ - opinion of software development tools which don't have a debugger.
    And yes I do know there are circumstance where you cannot
    use them and tool chains which don't support them, but don't expect
    me to sign a purchase requisition for such.

    By the way if this were C/C++ its the % opertor with -ve operands that I
    would not trust (same with the mod() function). I don't know if Java has
    sorted out the ambiquities in the definition for this case, but I know that
    I have been stung by the failure of C/C++ to comply with the defacto
    standard (Knuth's definition in TAOCP) in the past.

    RonL
    Last edited by CaptainBlack; October 27th 2006 at 12:10 AM.
    Follow Math Help Forum on Facebook and Google+

  11. #11
    Junior Member
    Joined
    Jul 2006
    Posts
    45
    Hi guys,

    I figured it out.

    The solution-- if ((number % 2) == 0)
    It was a precedence issue for some reason which I don't understand!!
    The % operator has higher precedence over the == operator, so what the (expletive deleted)
    Follow Math Help Forum on Facebook and Google+

  12. #12
    Senior Member
    Joined
    Apr 2006
    Posts
    399
    Awards
    1
    Quote Originally Posted by harold View Post
    Hi guys,

    I figured it out.

    The solution-- if ((number % 2) == 0)
    It was a precedence issue for some reason which I don't understand!!
    The % operator has higher precedence over the == operator, so what the (expletive deleted)
    It must be a bug in your Java compiler. There is no problem with

    if (number % 2 == 0)

    with my compiler.
    Follow Math Help Forum on Facebook and Google+

  13. #13
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    JakeD,

    I did not mean to cause any offence with my comments about debuggers re
    print statements. But if you have a lot of code to debug the edit compile run
    cycle can be incredibly in efficient compared to the use of the debugger, but
    even today I find people who should know better, using tools which have
    comprehensive debug facilities not using them and preferring to use print
    statements.

    RonL
    Follow Math Help Forum on Facebook and Google+

  14. #14
    Senior Member
    Joined
    Apr 2006
    Posts
    399
    Awards
    1
    Quote Originally Posted by CaptainBlack View Post
    JakeD,

    I did not mean to cause any offence with my comments about debuggers re
    print statements. But if you have a lot of code to debug the edit compile run
    cycle can be incredibly in efficient compared to the use of the debugger, but
    even today I find people who should know better, using tools which have
    comprehensive debug facilities not using them and preferring to use print
    statements.

    RonL
    RonL, thanks for your note and I hope I did not cause you any offence either.

    I have used a well-built commercial Java IDE and I have used the stock Java compiler available from Sun. Those are different worlds. With a good IDE, code is compiled on the fly as you type and bad code is immediately flagged with an error message. You run the debugger by clicking on the "Run Debugger" menu item. The debugger steps through your code and shows you all the values of the variables as it goes. If you have such an IDE, of course you should use the debugger as first recourse when you are puzzled by your code. I am not one of the people you describe, who would not click on Run Debugger because they are familiar with print statements.

    But judging from the error messages and questions students are asking in this and other help sites, students are not working with good IDEs. Instead they are in that second world of the stock Java or C++ compiler and no integrated debugger. Then using judiciously placed print statements is the main debugging tool. Moreover, the students are not aware of this basic debugging technique and so do not know how to figure out what their programs are doing. (I've also run into professional programmers in this boat.) So I think it is useful to tell them they can answer their questions using a few print statements. But I always mention to use a debugger if one is available.

    JakeD
    Last edited by JakeD; October 28th 2006 at 05:43 AM.
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. Java Blue J
    Posted in the Math Software Forum
    Replies: 4
    Last Post: September 5th 2010, 11:28 AM
  2. Help with Computer Science Java
    Posted in the Advanced Math Topics Forum
    Replies: 1
    Last Post: October 1st 2009, 05:24 PM
  3. Fixing The Program (Program Correctness)
    Posted in the Discrete Math Forum
    Replies: 2
    Last Post: March 21st 2009, 02:17 PM
  4. Another Java Program
    Posted in the Advanced Math Topics Forum
    Replies: 0
    Last Post: November 2nd 2006, 01:23 PM
  5. More Java Help
    Posted in the Advanced Math Topics Forum
    Replies: 1
    Last Post: October 27th 2006, 07:45 PM

Search Tags


/mathhelpforum @mathhelpforum