0

I'm having an issue when trying to count the number of even integers.

This is the code I'm working with:

int input=0, numeven=0;
Scanner scan = new Scanner(System.in);

input = scan.nextInt();

while (input != 0)
{
    //calculates the total number of even integers
    if (input%2 != 1)
    {
        numeven = numeven+1;
    }
}

I don't know how to set up the while loop: while (input! = 0)

Given the test input 6, 4, -2, 0 it says that I have three even numbers, but the expected outcome is 4 (because 0 is even).

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • 12
    It doesn't even check 0 because of `while (input != 0)` – GiantTree Jan 12 '16 at 18:46
  • 1
    As written here, this looks like an infinite loop if input != 0. scan.nextInt() is out of the while loop. Should it be in the while loop instead? – Auberon Jan 12 '16 at 18:50
  • How do I make it check every even number though? – gnollwarden Jan 12 '16 at 18:51
  • 1
    Use a value that's not going to be part of your data as a [sentinel value](https://en.wikipedia.org/wiki/Sentinel_value). -99 is a popular choice for student projects. – Bill the Lizard Jan 12 '16 at 18:51
  • 1
    How does that code even work? You are only getting one number from user and it is in 4th line. How can you count that you have 3 even numbers if you got only one number from user? Is this your real code? – ctomek Jan 12 '16 at 18:52
  • This is just a small part of my code, I end the while loop later on. There's actually a lot of different calculations that I have to do with the inputs. – gnollwarden Jan 12 '16 at 18:56
  • I have 3 other if statements that are supposed to calculate different numbers. – gnollwarden Jan 12 '16 at 18:57
  • I don't want my while loop to end on a certain value, I want it to look through all the possible numbers that I user can type in. – gnollwarden Jan 12 '16 at 19:00

3 Answers3

3

If you want your loop to work on zero, and treat it as the exit mark too, switch from while to do/while:

do {
    input = scan.nextInt();
    //calculates the total number of even integers
    if (input%2 != 1)
    {
        numeven = numeven+1;
    }
} while (input != 0);

This way your code will process zero along with regular inputs, and stop reading further input upon reaching the end of the loop.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • 1
    @KoebmandSTO Of course it doesn't. It's implied in the OP's program that he *wants* the input to stop upon reaching zero, so `4` will be ignored by his design. – Sergey Kalinichenko Jan 12 '16 at 19:14
  • Wanting to count 0s as even numbers imply you want to count them all. Else could just do a numeven++ upon end. – Koebmand STO Jan 12 '16 at 19:16
2

You don't want the loop to break when the user enters a 0 or any other integer incase you want to put 0 multiple times.

int numeven=0;
Scanner scan = new Scanner(System.in);

while (true) {
    String input = scan.next();
    try {
        int val = Integer.parseInt(input);
        if (val % 2 == 0)
            numeven++;

    } catch (NumberFormatException e) {
        //enter any input besides an integer and it will break the loop
        break;
    }
}

System.out.println("Total even numbers: " + numeven);

Alternatively this does the same thing. Except it won't consume the last value.

int numeven=0;
Scanner scan = new Scanner(System.in);

while (scan.hasNextInt()) {
    int val = scan.nextInt();
    if (val % 2 == 0)
        numeven++;
}

System.out.println("Total even numbers: " + numeven);
WalterM
  • 2,686
  • 1
  • 19
  • 26
-1

Just make the condition of your while loop to be

while( scan.hasNextInt() )

Then it will only loop as long as there are numbers. Inside the loop you can

input = scan.nextInt()
ganlaw
  • 1,303
  • 4
  • 18
  • 32