1

I'm having a bit of trouble with this do while loop. The system will print out the line and stop. What am I doing wrong? Thank you for your time!

Scanner keyboard = new Scanner(System.in);

    String answer;
    int inputNum = 1;
    int countOdd = 0;
    int countEven = 0;

    do{
        do{
            System.out.println("Please enter an interger. When you are finished, enter 0.");
            inputNum = keyboard.nextInt();

                while (inputNum % 2 == 0)
                    countOdd++;
                while (inputNum % 2 != 0)
                    countEven++;
        }while(inputNum != 0);


        System.out.println("Thank you. The amount of odd intergers you entered is " 
        + countOdd + ". The amount of even intergers you entered is " + countEven);


        System.out.println("Thank you. The amount of odd intergers you entered is " 
        + countOdd + ". The amount of even intergers you entered is " + countEven);

        System.out.println("Would you like to run this program again? (Y) (N)");
        answer = keyboard.nextLine();

    }while (answer.equals("Y"));

    System.out.println("Thank you!");
}

}

5
  • Do you ever close the outer {? Commented Oct 4, 2015 at 23:08
  • 1
    Never heard of a do loop, only a do while loop. Where's the while? Commented Oct 4, 2015 at 23:09
  • "The system will print out the line and stop" So what is it printing and what is your expected output? Commented Oct 4, 2015 at 23:14
  • Possible duplicate of Skipping nextLine() after using next(), nextInt() or other nextFoo() methods Commented Oct 4, 2015 at 23:18
  • ... After seeing @BalwinderSingh's answer, I see nextLine isn't the problem. (But it will be your next problem after you fix the while/if problem) Commented Oct 4, 2015 at 23:19

3 Answers 3

4

The following loops cannot be finished, since inputNum is not changed inside:

          while (inputNum % 2 == 0)
                countOdd++;
          while (inputNum % 2 != 0)
                countEven++;

You need if statements here instead of while.

Sign up to request clarification or add additional context in comments.

Comments

2

You need to change while with if. The logic inputNum%2==0 checks for even not odd.But if you did it intentionally opposite ,its fine. I changed it as it should be.

        Scanner keyboard = new Scanner(System.in);

    String answer;
    int inputNum = 1;
    int countOdd = 0;
    int countEven = 0;


        do{
            System.out.println("Please enter an interger. When you are finished, enter 0.");
            inputNum = keyboard.nextInt();

             if (inputNum % 2 == 0)
                 countEven++;
               if (inputNum % 2 != 0)
                   countOdd++;
        }while(inputNum != 0);


        System.out.println("Thank you. The amount of odd intergers you entered is " 
        + countOdd + ". The amount of even intergers you entered is " + countEven);

1 Comment

Why down voting?? its 100% correct answer. Plus the logic is also corrected.
1

The problem is with the following lines of code

while (inputNum % 2 == 0)
                countOdd++;
while (inputNum % 2 != 0)
                countEven++;

You don't use while statements for if conditions. Instead use this

 if (inputNum % 2 == 0)
                    countOdd++;
 if (inputNum % 2 != 0)
                    countEven++;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.