# Thread: Java Program - Summing Integers

1. ## 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);

}
}

2. Originally Posted by harold
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

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

4. Originally Posted by harold
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

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

6. Originally Posted by harold
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

7. 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);
}
}

8. Originally Posted by harold
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

9. Originally Posted by CaptainBlack
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);

10. Originally Posted by JakeD
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

*%&#163;\$**&#163; - 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

11. 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)

12. Originally Posted by harold
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.

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

14. Originally Posted by CaptainBlack
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