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.


>> REGISTER NOW TO START POSTING


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

Results 1 to 11 of 11

Thread: adding up odd and even numbers

  1. #1
    Junior Member
    Join Date
    Sep 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

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


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: adding up odd and even numbers

    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.

  3. #3
    Junior Member
    Join Date
    Sep 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: adding up odd and even numbers

    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?

  5. #5
    Junior Member
    Join Date
    Sep 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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
    Last edited by darlinho; September 30th, 2010 at 02:39 PM.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

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

  7. #7
    Junior Member
    Join Date
    Sep 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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?

  8. #8
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Post Re: adding up odd and even numbers

    Quote Originally Posted by darlinho View Post
    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.

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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.
    	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.
    Last edited by Norm; September 30th, 2010 at 03:22 PM.

  10. #10
    Junior Member
    Join Date
    Sep 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: adding up odd and even numbers

    we are not allowed to make any changes to io.java file.

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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!!!
    Last edited by Norm; September 30th, 2010 at 03:52 PM.

Similar Threads

  1. Java program to do Matrix operation
    By saladfingers73 in forum Collections and Generics
    Replies: 5
    Last Post: March 7th, 2012, 09:17 AM
  2. [SOLVED] What is not right here? adding JPanel in JFrame
    By Asido in forum AWT / Java Swing
    Replies: 2
    Last Post: August 23rd, 2010, 08:16 AM
  3. Adding panels to a central panel.
    By Johannes in forum AWT / Java Swing
    Replies: 3
    Last Post: July 4th, 2010, 05:31 PM
  4. [SOLVED] Problem with a tutorial program(Adding the answer of two squared numbers together)
    By Melawe in forum What's Wrong With My Code?
    Replies: 20
    Last Post: April 7th, 2010, 09:03 AM
  5. [SOLVED] adding items to a binary tree
    By vendetta in forum Algorithms & Recursion
    Replies: 4
    Last Post: February 26th, 2010, 09:32 PM