1

Below is the code to find 0 in a number for

Example : 1701 has 0, 1711 has no zero.If the number is a single digit it will print "Not possible".

The problem of the code is that if the number is like 666 then 'solve(num)' get executed 3 times whereas it should be executed only once and if the number is 505 then 'solve num' gets executed once whereas it shouldn't be executed as the number 505 has zero in it.

private void match(int num) {
    int length=(int) (Math.log10(num)+ 1);
    if(length==1){
        System.out.println("Not possible");
    }
    else{
       if(num%10!=0){
           char n[]=new char[length];
           String number = String.valueOf(num);
           char[] digits1 = number.toCharArray();
            for(int i=0;i<number.length();i++){
            //  n[i]=(Character.valueOf((char) digits1[i]));
              if((Character.valueOf((char) digits1[i]))!='0'){
                  solve(num);
              }

            } 
       }
    }
  } 
Durgpal Singh
  • 11,481
  • 4
  • 37
  • 49
Fawkes
  • 105
  • 1
  • 9
  • well for sure it gets executed. Why shouldn´t it if you execute `solve` for every digit not beeing a `0`? – SomeJavaGuy Aug 26 '16 at 06:06
  • Start by making a separate method, taking a string as argument, and returning true if the string contains the char '0', and false otherwise. Then use that method in match(). – JB Nizet Aug 26 '16 at 06:06
  • So you want to call `solve(num)` exactly once in the case that `num` does _not_ have a 0 in its decimal representation and to not call it at all if it does? – Ted Hopp Aug 26 '16 at 06:06
  • Hmmm why not just use `Integer.toString()` and `String.indexOf()`? – NullUserException Aug 26 '16 at 06:07
  • @TedHopp Yes. if the number is 505 then solve(num) should not gets called but if the number is 555 then it should be executed once. – Fawkes Aug 26 '16 at 06:09

4 Answers4

2

If you are looking for an efficient way to check if a string contains a certain digit, use String.indexOf() (or maybe String.contains(); but the first one gives better performance).

There is absolutely no need to work on such a low-level! To the contrary: good programming is about using helpful abstractions. You absolutely want to minimize the number of loops, ifs, ... in your code. Simply because 5 lines of code ... can contain more bugs than 1 line of code; especially if those 5 lines are a "hand written re-invention" of that wheel that you can perfectly call in 1 line; and that is guaranteed to work.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • 1
    of rather String.contains ? – Scary Wombat Aug 26 '16 at 06:07
  • @ScaryWombat - when looking for a single character, using `String#indexOf(int)` to find it is going to be much faster. – Ted Hopp Aug 26 '16 at 06:08
  • @TedHopp that doesn´t make much sense to me, doesn´t `String#contains` just return `String#indexOf > -1` – SomeJavaGuy Aug 26 '16 at 06:10
  • @KevinEsche - But it calls it with a `CharSequence` argument. Calling the single-character method will be faster, because it actually calls a native method. – Ted Hopp Aug 26 '16 at 06:11
  • @TedHopp The implementation of `indexOf()` is most likely pure Java in most JVMs: http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/lang/String.java#String.indexOf%28java.lang.String%2Cint%29 – NullUserException Aug 26 '16 at 06:14
  • @NullUserException that doesn't mean there isn't an intrinsic method in the JVM that is uses rather than the Java implementation, just for performance. – JB Nizet Aug 26 '16 at 06:17
  • @NullUserException - Well, in Android, it's a native method. And even if it's pure Java, `indexOf('0')` is going to be faster than `indexOf("0")` (or `contains("0")`). – Ted Hopp Aug 26 '16 at 06:23
  • `There should be a rule that you have to upvote an answer after making 3 comments ` - Damn – Scary Wombat Aug 26 '16 at 06:51
2

Just convert the int to String, and use indexOf() like this:

int num = 1701;
String s = String.valueOf(num);
if (s.indexOf('0') > 0){
    your code
}

References:

Java - Convert integer to string

String.indexOf()

Community
  • 1
  • 1
Raskayu
  • 735
  • 7
  • 20
  • 1
    Or better yet, use `s.indexOf('0')`. Searching for a single character is faster than searching for a one-character string sequence. – Ted Hopp Aug 26 '16 at 06:10
1

Just break

  if (.....) {
      solve (num);
      break;
  }

An easier way would be

  private void match(int num) {

    String str = String.valueOf(num);
    if(str.length () <=1){
      System.out.println("Not possible");
      return;
    }
    if(str.indexOf ('0') > 0){
      solve(num);
    }

  } 
Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
1

String#contains(...) uses String#indexOf(...), so is better and more intuitive for development...

public static void main(String[] args) {
    for (int i = 1; i < 15; i++) {
        check(i);
    }
}
private static void check(int i) {
    System.out.println(String.valueOf(i).contains("0"));
}
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97