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!");
}

}

  • Do you ever close the outer `{`? – intcreator Oct 04 '15 at 23:08
  • 1
    Never heard of a do loop, only a do while loop. Where's the while? – mmking Oct 04 '15 at 23:09
  • `"The system will print out the line and stop"` So what is it printing and what is your expected output? – user3437460 Oct 04 '15 at 23:14
  • Possible duplicate of [Skipping nextLine() after using next(), nextInt() or other nextFoo() methods](http://stackoverflow.com/questions/13102045/skipping-nextline-after-using-next-nextint-or-other-nextfoo-methods) – user253751 Oct 04 '15 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) – user253751 Oct 04 '15 at 23:19

3 Answers3

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.

Orest Hera
  • 6,346
  • 2
  • 17
  • 33
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);
App Work
  • 20,984
  • 5
  • 23
  • 38
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++;
Balwinder Singh
  • 2,196
  • 5
  • 20
  • 32