Checking an integer to see if it contains zero

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

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

How can I do that?

+6
java integer parsing
source share
7 answers

Do you mean if the decimal notation contains 0? The absolute easiest way to do this:

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

Do not forget that the number is not "in fact" contains 0 or not (except for zero itself, of course) - it depends on the base. So, β€œ10” in decimal β€œA” in hexadecimal, and β€œ10” in hexadecimal β€œ16” in decimal ... in both cases, the result has changed.

There may be more effective ways to test for the presence of zero in the decimal representation of an integer, but they are likely to be significantly more involved in the expression above.

+33
source share

If for some reason you don't like the solution that converts to 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 also assumes num is base 10.

Edit: Added conditions for working with negative numbers and 0 themselves.

+22
source share

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

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

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

+1
source share

Here is a routine that will work with detecting zeros in integers. To make it work with any representation (decimal, hexadecimal, octal, binary), you need to pass 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
source share

I don’t know if this is easier, but here is another way. Divide the number by an int array. Then sort and check if the first element is zero. For example,

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

Not using Java, but it is not at all difficult 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; } 
0
source share

All Articles