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?
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?
If for some reason you don't like the solution that converts to a String you can try:
This is also assuming
num
is base 10.Edit: added conditions to deal with negative numbers and 0 itself.
Not using Java, but it's not exactly hard to convert from C++ PS. Shame on anyone using string conversion.
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.
Integer.toString(yourIntValue).contains("0");
You can convert it to a string and check if it contains the char "0".
Do you mean if the decimal representation contains a 0? The absolute simplest way of doing that is:
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.