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)");
int n = IO.readInt();
while (n<1){
System.out.print("Number must be >=2");
n = IO.readInt();
}
int counteven=0,countodd=0;
System.out.print("Enter first number");
int number = IO.readInt();
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.
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.
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
* outputDoubleAnswer: sends your real-number output to the screen and to our grading program
* outputIntAnswer: sends your integer output to the screen and to our grading program
* outputBooleanAnswer: sends your boolean output to the screen and to our grading program
* outputStringAnswer: sends your string output to the screen and to our grading program
* outputCharAnswer: sends your single-character output to the screen and to our grading program
* reportBadInput : reports an error condition by printing a message on the screen and by sending a message to the grading program
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?
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
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.
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?
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)");
int n = IO.readInt();
while (n<1){
System.out.print("Number must be >=2");
n = IO.readInt();
}
int counteven=0,countodd=0;
System.out.print("Enter first number");
int number = IO.readInt();
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.
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.
Re: adding up odd and even numbers
we are not allowed to make any changes to io.java file.
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!!!