Вопрос

I am writing a code to convert a number to binary representation.Here's my code.It doesn't give the correct answer but I can't figure out where I am making the mistake.If someone could point out where my mistake and how to correct it I would be grateful.

public class ConvertBinary{
    public static String convertBinary(int n){
        String s="";
        while(n>0){
            s+=(n%2);

            n=n/2;
        }


        int len=s.length();
        String[] binary=s.split("");
        String[] binaryCopy=new String[s.length()];

        for(int i=0;i<len;i++){
            binaryCopy[i]=binary[len-i-1];
            s+=binaryCopy[i];
        }
        return s;

    }

    public static void main (String args[]){
        System.out.println(convertBinary(19));
    }
}
Это было полезно?

Решение

Apart from all these answers the problem with your code was that you are not clearing the string before reversing it. So before the for loop just put s = "" and you code should work fine.. :)

Based on comment

public class ConvertBinary {
    public static String convertBinary(int n) {
        String s = "";
        while (n > 0) {
            s += (n % 2);

            n = n / 2;
        }

        int len = s.length();
        String[] binary = s.split("");
        String[] binaryCopy = new String[s.length()];

        s = "";

        for (int i = 0; i < len; i++) {
            binaryCopy[i] = binary[len - i - 1];
            s += binaryCopy[i];
        }
        return s;

    }

    public static void main(String args[]) {
        int num = 4;
        System.out.println(convertBinary(num));
        System.out.println(Integer.toBinaryString(num));
    }
}

Другие советы

public static String convertBinary(int n){
        String s="";
        while(n>0){
            s+=(n%2);

            n=n/2;
        }



    return (new StringBuffer(s).reverse().toString());
}

If you're looking for error in your implementation, you'd rather put:

  s = (n % 2) + s;

isntead of

  s+=(n%2);

so the code'll be

  // n should be positive
  public static String convertBinary(int n){
    if (n == 0)
      return "0";

    String s = "";

    // for is more compact than while here
    for (; n > 0; n /= 2)
      s = (n % 2) + s;

    return s;
  }

however in real life

  Integer.toString(n, 2);

is much more convenient

Use Java standard library: http://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#toString%28int,%20int%29

public class ConvertBinary{
    public static String convertBinary(int n){
        return Integer.toString(n, 2);
}

    public static void main (String args[]){
        System.out.println(ConveryBinary.convertBinary(19));
    }
}

EDIT: As @Holger says, there's also a toBinaryString: http://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#toBinaryString%28int%29

public static String convertBinary(int n){
    return Integer.toBinaryString(n);
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top