15

Given an integer, how could you check if it contains a 0, using Java?

1 = Good
2 = Good
...
9 = Good
10 = BAD!
101 = BAD!
1026 = BAD!
1111 = Good

How can this be done?

7 Answers 7

46

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.

1
  • 6
    +1 - I would use this solution in production code until someone proved to me that a) it was a bottleneck, and b) another solution was significantly faster. There's a lot to be said for simplicity and readability. Oct 2, 2010 at 21:05
26

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.

1
  • 1
    Well Done it's solve my problem and also well explained Mar 11, 2022 at 7:09
2

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" );
}
3
  • 8
    I really dislike the "" + x way of converting a value to a string. It doesn't express the intention clearly at all - you're not really interested in concatenation after all... you're after a conversion. Just MHO of course.
    – Jon Skeet
    Oct 2, 2010 at 20:53
  • I admit, it's ugly. But it's very easy to remember.
    – tangens
    Oct 2, 2010 at 20:56
  • Is String.valueOf() more difficult to remember? Oct 3, 2010 at 9:41
2

Integer.toString(yourIntValue).contains("0");

1

Here is a routine that will work detect zeros in integers. To make it work with any representation (decimal, hex, octal, binary), you need to pass in the base as a parameter.

public static boolean hasZero(int num, int base) {
    assert base > 0 : "must have positive non-zero base";

    if (num == 0)
        return true;

    while(num != 0) {
        if (num % base == 0) {
            return true;
        }
        else {
            num = num / base;
        }
    }

    return false;
}

public static void main(String args[]) {
    System.out.println(hasZero(10, 10));  // true (base 10 int)
    System.out.println(hasZero(-12, 10));  // false (base 10 int)

    System.out.println(hasZero(0x10, 16)); // true (hex is base 16)
    System.out.println(hasZero(0x1A, 16)); // false (hex is base 16)
}
1
  • Almost duplacate to the answer above
    – Younes
    Oct 4, 2010 at 13:51
0

I don't know if this is easier but here is another way. Split the number into an array of ints. Then sort and check if the first element is zero. E.g

int n = 14501;
// after splitting
int na = {1, 4, 5, 0, 1};
// after sorting
int na = {0, 1, 1, 4, 5};
0
-1

Not using Java, but it's not exactly hard to convert from C++ PS. Shame on anyone using string conversion.

bool Contains0InBase10( unsigned int i, unsigned int& next )
{
 unsigned int divisor = 10;
 unsigned int remainder = 0;
 while( divisor <= i )
 {
  unsigned int newRemainder = i%divisor;
  if( newRemainder - remainder == 0)
  {
   // give back information allowing a program to skip closer to the next
   // number that doesn't contain 0
   next = i + (divisor / 10) - remainder;
   return true;
  }
  divisor *= 10;
  remainder = newRemainder;
 }
 return false;
}
1
  • 1
    While this algorithm may be inherently more efficient than a string conversion, a little explanation on why the modulus operator will do what the OP is looking for may clarify your post.
    – Sean
    Oct 3, 2010 at 2:31

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.