0

Hey I'm really trying to convert decimal int to binary int, without succes.

Please help me.

And I don't want to make it 'System.out.println()' becuase I've already done it.

THANKS!

`I need recursive function that gets decimal int and return binary int

public static void decToBin(int number) {
    if(number == 0)
        return ;
    decToBin(number / 2);
    System.out.print(number % 2);
}

That it what I've done...

When I'm trying to get string:

public static String decToBin(int number) {
    if(number == 0)
        return "";
    return new Integer(number % 2).toString() + new Integer(decToBin(number / 2)).toString();
}

Error...

5
  • 2
    Please show us what you've attempted. Commented May 25, 2014 at 12:00
  • 1
    And what is the problem with your code? Do you get some errors/incorrect results? Or are you just asking us to rewrite it into something else which doesn't use System.out.println? Commented May 25, 2014 at 12:09
  • Yes, I want it to be string or int... Look my edit again please. Commented May 25, 2014 at 12:11
  • Why do you need recursion? Commented May 25, 2014 at 12:13
  • Do you have to use String as result? StringBuilder would be easier to work with. Commented May 25, 2014 at 12:16

4 Answers 4

2
public static String decToBin(int number) {
    if(number == 0)
        return "";
    return new Integer(number % 2).toString() + new Integer(decToBin(number / 2)).toString();
}

This approach has few flaws.

  • First you can't use "" as argument of new Integer("") because "" holds no value, so you will see NumberFormatException.

  • Other problem is order of generating result. number % 2 should be placed after result of decToBin(number / 2), just like you are doing it in your first example, where you are printing it after recursive decToBin call

    decToBin(number / 2);
    System.out.print(number % 2);
    
  • Lastly in your return statement you are creating new string, so to generate "100101" you will generate "" "1" "01" "101" 0101" "00101" and then finally "100101". To avoid it use StringBuilder and its append method.

Sign up to request clarification or add additional context in comments.

2 Comments

This converts a binary integer, not a decimal number.
@EJP Hello. Could you clarify your comment? I am not saying you are wrong but I am not sure what you mean. From what I see question is about creating binary representation of integer provided in decimal system (I am not sure if OP wants to return String or int here so didn't focus on that). Also this answer wasn't meant to provide working solution (since it is clearly homework question about recursion) but to point few mistakes which OP made in his code.
1

This is a little old, but for the sake of reference and giving a correct answer, this is what you would need to do:

public static String dec2Bin(int num) {

    String result = ((num % 2 == 0) ? "0" : "1");

    if (num == 0 || num == 1) {
        return result;
    }

    return dec2Bin(num/2) + result;
}

1 Comment

This converts a binary integer, not a decimal number.
0
    void intToBinary(int num) {

    if (num < 2) {
        System.out.print(num);
        return;
    } else {
        intToBinary(num / 2);
        intToBinary(num % 2);
    }

}

1 Comment

This converts a binary integer, not a decimal number.
0
import java.io.*;

class Binary{
    public static void main(String args[])throws IOException{
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Enter the number:");
        int n = Integer.parseInt(br.readLine());
        System.out.print(Integer.toBinaryString(n));
    }
}

1 Comment

The code example is nice, but no clarification as to what it does. Should explain the answer (really, Integer.toBinaryString(n) is what the author wants I think).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.