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 17 of 17

Thread: Odd/Even

  1. #1
    Junior Member
    Join Date
    Feb 2010
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Odd/Even

    Thanks all.
    Last edited by SOK; September 27th, 2010 at 07:41 PM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Odd/Even

    not sure what to do to make my program do it all over without ending.
    Use a loop that does not exit until the user says OK by entering the special value.

    What does your code currently do? I see a while loop that tests for the ending value. Does that work as you want?

    Please put your code inside of code tags. Info here: Java Forums - BB Code List

  3. #3
    Junior Member
    Join Date
    Feb 2010
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Odd/Even

    Quote Originally Posted by Norm View Post
    Use a loop that does not exit until the user says OK by entering the special value.

    What does your code currently do? I see a while loop that tests for the ending value. Does that work as you want?

    Please put your code inside of code tags. Info here: Java Forums - BB Code List
    The while loop does work how I want it so far. Basically I enter an integer, if it is not equal to 0 then I keep enter odd and even integers. As soon as I enter 0, the program outputs how many odd and even numbers I chose, it could be 1,2,3,4,5,6 or more numbers. My problem is when I choose 0, it does output the amount of odd and even numbers, and does ask if I want to do it again, but then how I have my code the program ends after it asks me. I am not to sure how to get it to it all over again after I enter yes to continue.

    Basically the program is to continue until I type no. So I could enter one odd number and then enter 0 get the below output

    There are 1 odd numbers in the sequence
    There are 0 even numbers in the sequence
    Do you wish to enter another sequence (yes/no)?Yes

    Program continues and asks me for an integer, and I keep entering until I type 0

    I enter 5 odd and 2 even numbers output would be:

    There are 5 odd numbers in the sequence
    There are 2 even numbers in the sequence
    Do you wish to enter another sequence (yes/no)?no

    When I choose no the program should terminate.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Odd/Even

    I assume that the value of the askUser variable is false when the user enters: "no".
    What does your code do when the value of askUser is false?
    You didn't post the rest of the code in while loop. You seem to have changed the original code to something new and have left off some of it.

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

    Default Re: Odd/Even

    That is all the code I have right now. If I post the rest of it in the while loop, will it not always print out everything in it every time I enter a number? I am sure I tried putting it in all in the while loop and every time I entered a number it would print the odd and even and ask me if I wanted to enter another sequence, even though I did not type 0.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Odd/Even

    Can you post what is printed on the console when you execute the program?
    Add some comments to that output showing where you want the program to work differently.

    Also post the full code that you are using. What you have posted now does not compile.


    The formatting of your code is very poor.
    Please remove the extra {} and line up the {} that you have
    and indent the code inside of the {} to show what level the code is nested at.
    Last edited by Norm; September 26th, 2010 at 10:56 AM.

  7. #7
    Junior Member
    Join Date
    Feb 2010
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Odd/Even

    enter an integer:5
    enter an integer:4
    enter an integer:5
    enter an integer:5
    enter an integer:4
    enter an integer:0
    There are 3 odd numbers in the sequence
    There are 3 even numbers in the sequence
    Do you wish to enter another Sequence?(Yes/No)?Yes
    enter an integer:5
    program ends.

    You will not be able to run the program. Our teacher gave us an input helper that they made so we do not have to error check, it does it for us.

    That is why you see InputHelper.askUserForAnInt in the program.

  8. #8
    Junior Member
    Join Date
    Feb 2010
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Odd/Even

    I would have to send you the InputHelper file for you to compile the program.

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Odd/Even

    I can replace the InputHelper without no problem.
    But I can't help you with your code without seeing your code.

    Why does the program end after the user enters the 5?
    What code is supposed to prevent the exit of a loop? Is there a variable that needs to be reset?

  10. #10
    Junior Member
    Join Date
    Feb 2010
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Odd/Even

    Quote Originally Posted by Norm View Post
    I can replace the InputHelper without no problem.
    But I can't help you with your code without seeing your code.

    Why does the program end after the user enters the 5?
    What code is supposed to prevent the exit of a loop? Is there a variable that needs to be reset?
    The program is not supposed to end at 5, it just does, one of the issues I have.

    Here is the spec that I need to do.

    Functional Requirements of Finished Program

    At the start of the program, the user is to be prompted with:
    Enter an integer:
    If the user types a non-zero integer, the program must respond with the prompt:
    Enter an integer:
    If the user types number 0 the program prints the number of odd and even integers in the sequence excluding 0 as follows:
    There are XX odd numbers in the sequence.
    There are XX even numbers in the sequence.
    ... where XX is replaced by the appropriate value.
    After this the program must continue to prompt with:
    Do you wish to enter another sequence (yes/no)?
    The net effect is that the user is continually prompted to enter sequences of integers, until he or she wishes to stop. Note that the user is allowed to enter a sequence of 1, 2, 3, or more numbers. There is no limit to the length of the sequence. Note that the user is allowed to enter 1, or more sequences of numbers. Any integer is legal, including negative numbers. 

  11. #11
    Junior Member
    Join Date
    Feb 2010
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Odd/Even

    I guess the main issue I am having is, the while loop works, and as soon as you type 0 the program stops to give you the number of odd and even numbers and then asks you if you want to enter another sequence. If you say no the program ends. The problem I have is how to I initialize it back into the loop, or do I need another loop, if I type yes to enter a new sequence how do I use that while loop again, since Java starts from top to bottom, how do I re-use that loop after saying yes to enter a new sequence so it does all the adding up again.

  12. #12
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Re: Odd/Even

    Just put all the code inside a loop.

    boolean loopDone = false;
    While (!loopdone) {
       //Odd Even code here
     
       //if user says stop program then:
          loopDone = true;
    }

  13. #13
    Junior Member
    Join Date
    Feb 2010
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Odd/Even

    Thanks all.
    Last edited by SOK; September 27th, 2010 at 07:40 PM.

  14. #14
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Re: Odd/Even

    //just move
    int oddNum = 0;
    int evenNum = 0;
     
    //below this line
    while (askUser == true)
    {

  15. #15
    Junior Member
    Join Date
    Feb 2010
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Odd/Even

    Quote Originally Posted by Brt93yoda View Post
    //just move
    int oddNum = 0;
    int evenNum = 0;
     
    //below this line
    while (askUser == true)
    {
    Thanks and thanks to all that helped, program is working to spec.

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

    Wink Re: Odd/Even

    This is the answer for your problem.
    try, this code it is going exactly what you want...


    import java.io.*;
    public class number
    {
    public static void main(String[] args)throws IOException
    {
    boolean askUser;
    int enterNum;
    int oddNum = 0;
    int evenNum = 0;

    DataInputStream d=new DataInputStream(System.in);
    System.out.println("enter a number other than 0 -- : ");
    enterNum = Integer.parseInt(d.readLine());

    while (true)
    {


    if (enterNum % 2 == 0)
    {
    evenNum ++;

    }
    else
    {
    oddNum ++;
    }


    System.out.println("do you wish to continue--enter (true/false)");
    askUser = Boolean.parseBoolean(d.readLine());

    if (askUser == true)
    {
    System.out.println("enter a number: ");
    enterNum = Integer.parseInt(d.readLine());

    }
    else
    {
    System.out.println("There are " + oddNum + " odd numbers in the sequence");
    System.out.println("There are " + evenNum + " even numbers in the sequence");
    System.exit(0);
    }
    }
    }
    }





    thanks,
    Pratul

  17. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Odd/Even

    @pratul
    When you post code solving a students problem, can you add comments to it explaining how it works and why.

    Also please put your code in code tags. Info here: Java Forums - BB Code List