checking an integer to see if it contains a zero

If for some reason you don't like the solution that converts to a String you can try:

boolean containsZero(int num) {
    if(num == 0)
        return true;

    if(num < 0)
        num = -num;

    while(num > 0) {
        if(num % 10 == 0)
            return true;
        num /= 10;
    }
    return false;
}

This is also assuming num is base 10.

Edit: added conditions to deal with negative numbers and 0 itself.


Do you mean if the decimal representation contains a 0? The absolute simplest way of doing that is:

if (String.valueOf(x).contains("0"))

Don't forget that a number doesn't "inherently" contain a 0 or not (except for zero itself, of course) - it depends on the base. So "10" in decimal is "A" in hex, and "10" in hex is "16" in decimal... in both cases the result would change.

There may be more efficient ways of testing for the presence of a zero in the decimal representation of an integer, but they're likely to be considerably more involved that the expression above.


You can convert it to a string and check if it contains the char "0".

int number = 101;
if( ( "" + number ).contains( "0" ) ) {
  System.out.println( "contains the digit 0" );
}