# adding up odd and even numbers

• September 30th, 2010, 11:27 AM
darlinho
adding up odd and even numbers
hey guys i a m a beginner to programming (trying to become a CS major). so far i feel like in the group of people in class who are behind with the rest of the class. Most of these kids seem to have prior knowledge in programming i do not.

Any helpful hints for me to use to become better at programming and to also Ace this class would be much appreciated.

Anywaysi am trying to develop a program that will count the number of even and odd integers in a set ("even" meaning divisible by 2, "odd" meaning not divisible by 2). zero should be used as an indicator that the set has been completely entered, and this zero should not be counted as part of the set

so far this is what i have come up with.

public class EvenOdd {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.print("Enter number of values (int>=1)");
while (n<1){
System.out.print("Number must be >=2");

}
int counteven=0,countodd=0;
System.out.print("Enter first number");
if (number % 2 == 0){
counteven++;
}else if (number %2 !=0);{
countodd++;

System.out.println("count of even numbers is " + counteven + ".");
System.out.println("count of odd numbers is " + countodd + ".");

I am using eclipse. When i run the program and enter "2" it says not an integer and to try again. then when i enter ten it tell me that my count for even is 1 and count for odd is 1 as well. This should not happen.
• September 30th, 2010, 01:28 PM
Norm
Re: adding up odd and even numbers
Quote:

enter "2" it says not an integer and to try again
What/who is the "it" that says the message?

Can you execute the program and copy the console and paste it here so we can see what was entered and what the messages are?

What packages are you importing. I don't see a definition for the IO class.
• September 30th, 2010, 02:13 PM
darlinho
Re: adding up odd and even numbers
this is the console. I am using Eclipse

Enter number of values (int>=1)4
Enter first number 4
That is not an integer. Enter again: 10
count of even numbers is 1.
count of odd numbers is 1.

i did not import anything... i have to use the IO.module for my class. this is what it

The IO module supports the following operations:

* readDouble : lets the user enter a real number and returns it
* readInt : lets the user enter an integer and returns it
* readChar : lets the user enter a single character and returns it
* readString : lets the user enter some text, and returns it as a string
* readBoolean: lets the user make a choice and returns true for Yes and false for No

* reportBadInput : reports an error condition by printing a message on the screen and by sending a message to the grading program
• September 30th, 2010, 02:19 PM
Norm
Re: adding up odd and even numbers
Quote:

That is not an integer. Enter again: 10
Where does that message come from? I don't see it in the code you posted.

How does your compiler find the definition of the IO class? It is not part of the standard JDK.
Do you have a special IDE that automatically adds classes that are NOT part of the standard JDK?
• September 30th, 2010, 02:36 PM
darlinho
Re: adding up odd and even numbers
i did not import anything... i just used the io module. maybe the the reason it says that is because i did not put the Io. output or anything for the input.

i had the io.java file put in the source folder where my .class file of the program is.

i could not attach the file here to you but its from here CS111 Home Page

you go to tools and click Io module
• September 30th, 2010, 02:40 PM
Norm
Re: adding up odd and even numbers
If you have the source for the IO.java file, look in it and see if the error message comes from there.
If it does, then your problem could be inside of the IO.java file.
• September 30th, 2010, 03:00 PM
darlinho
Re: adding up odd and even numbers
the io.file was made my the teacher so i dont think the error can be from there. is the rest of my program ok? is that how you would have attacked this problem?
• September 30th, 2010, 03:06 PM
javapenguin
Re: adding up odd and even numbers
Quote:

Originally Posted by darlinho
hey guys i a m a beginner to programming (trying to become a CS major). so far i feel like in the group of people in class who are behind with the rest of the class. Most of these kids seem to have prior knowledge in programming i do not.

Any helpful hints for me to use to become better at programming and to also Ace this class would be much appreciated.

Anywaysi am trying to develop a program that will count the number of even and odd integers in a set ("even" meaning divisible by 2, "odd" meaning not divisible by 2). zero should be used as an indicator that the set has been completely entered, and this zero should not be counted as part of the set

so far this is what i have come up with.

public class EvenOdd {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.print("Enter number of values (int>=1)");
while (n<1){
System.out.print("Number must be >=2");

}
int counteven=0,countodd=0;
System.out.print("Enter first number");
if (number % 2 == 0){
counteven++;
}else if (number %2 !=0);{
countodd++;

System.out.println("count of even numbers is " + counteven + ".");
System.out.println("count of odd numbers is " + countodd + ".");

I am using eclipse. When i run the program and enter "2" it says not an integer and to try again. then when i enter ten it tell me that my count for even is 1 and count for odd is 1 as well. This should not happen.

Maybe you could use this:

That an odd integer will be expressed as 2k+1 where k is an integer.
An even integer will be expressed as 2m where m is an integer.

Also, calling it directly by the class name means it's static or something like that.
• September 30th, 2010, 03:19 PM
Norm
Re: adding up odd and even numbers
OK, I looked at the IO.java program and that is where the error message is coming from. The code is a little too loose to suit me.
It should show you what you typed in so you can understand what the problem is.

If you can make a temporary change to the IO.java file, try this:
Replace the readInt method code with this. Comment out the current version and add this. When done, you can remove this code and remove the comments.
Code :

``` public static int readInt() { String s = null; while (true) { try { s = kb.readLine(); return Integer.parseInt(s); } catch (NumberFormatException e) { System.out.print("s=" + s + "< is not an integer. Enter again: "); // Show invalid String in message } catch (IOException e) { // should never happen } } }```

Then compute it and run your program again. The error message should show what the bad input is.
Probably an extra space. The readInt() method above could use the String trim() method to clean that up.
• September 30th, 2010, 03:21 PM
darlinho
Re: adding up odd and even numbers
we are not allowed to make any changes to io.java file.
• September 30th, 2010, 03:28 PM
Norm
Re: adding up odd and even numbers
Not even for testing??? You'll change it back after the test is done.
That is where the error was detected. Until you understand why readInt gives the error, I have no suggestions how to proceed.

One thought, when you enter a number do NOT enter any spaces!!!