0

I've been working on a recursive solution for assessing whether a number is binary or not. However my solution always returns false and I can't seem to get the logic correct. My code is below:

public class Convert{
  public static boolean isBinaryNumber(int binary){
    int temp = 0; 
    boolean status = false;

    if(binary==0 || binary==1) {
        status = true;
        return status;
    }
    else {
        temp =  binary%10;

        if(temp == 1 || temp == 0) {
            binary = binary/10;
            isBinaryNumber(binary);
        }
        else {
            status = false;
            return status;
        }
    }
    return status;
  }

  public static void main(String a[]){
    System.out.println("Is 1000111 binary? :"+ isBinaryNumber(1000111));
    System.out.println("Is 10300111 binary? :"+ isBinaryNumber(10300111));
  }
}
0

2 Answers 2

4
    if(temp == 1 || temp == 0) {
        binary = binary/10;
        isBinaryNumber(binary);
    }

should take the return value of the recursive call into account, e.g.

    if(temp == 1 || temp == 0) {
        binary = binary/10;
        status = isBinaryNumber(binary);
    }

Note that the whole code can be compressed quite a bit, for instance you could remove either the status variable or the early return statements consistently.

If you keep status and you explicitly cover all cases, I'd remove the initialization to false, so the compiler is able to point out branches with missing assignments. This would probably have pointed to the error location in this case.

With some further simplifications, the code could be compressed to

    public static boolean isBinaryNumber(int binary) {
       return binary == 1 || binary == 0 || binary >= 10 &&
           isBinaryNunber(binary / 10) && isBinaryNumber(binary % 10)
    }
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you very much. Could you possibly explain why that is?
Before, you were ignoring the result of the recursive call. The assignment to status makes sure it's taken into account and returned
Your "compressed" solution is not correct since it loops.
1

You have to put a return in front of your recursive call. The rest of the code would work as expected, if you remove the last return statement.

However, I did some optimizations (described below). Therefore, the Code should look like this:

public static boolean isBinaryNumber(int binary){
    if(binary==0 || binary==1) {
        return true;
    }
    else {
        int temp =  binary%10;
        if(temp == 1 || temp == 0) {
            binary = binary/10;
            return isBinaryNumber(binary);
        }
        else {
            return false;
        }
    }
}

I also removed the useless status variable, since you can simply return the results instead of storing it to a value and than return it.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.