-2

Is this correct? I am trying to set this up as an interview question and need help with the correct answer. If anyone can take a look at the code below and let me know if this is the correct answer, and if there are other correct answers than just the one below that would be great.

The question is "Create a Program That Will Convert Decimal Number into Binary Format" Using JAVA

package com.java2novice.algos;

public class DecToBin {

    public void showBinaryFormat(int numbers){
        int binaryNo[] = new int[25];
        int indexNo = 0;
        while(number > 0){
            binaryNo[index++] = numbers%2;
            numbers = numbers/2;
        }
        for(int i = indexNo-1;i >= 0;i--){
            System.out.print(binaryNo[i]);
        }
    }

    public static void main(String a[]){
        DecimalToBinary dtb = new DecimalToBinary();
        dtb.showBinaryFormat(25);
    }
}
mhasan
  • 3,703
  • 1
  • 18
  • 37
  • 2
    **Is this correct?** - Run it, test it and you'll know if it's correct. Instead, if you have any specific error paste it in the question with the complete stacktrace. I see at least one compilation problem here. – BackSlash Oct 25 '16 at 19:48
  • 1
    You could get help from codereview.stackexchange.com – SomeDude Oct 25 '16 at 19:49
  • 1
    What @BackSlash said, plus `indexNo` and `index` are not the same variable. – Jonny Henly Oct 25 '16 at 19:49
  • I will tell you right now it is not correct: you have `while (number > 0) {` but `number` is undefined – ControlAltDel Oct 25 '16 at 19:50
  • @svasa: The OP should still determine if it works first. We're not responsible for determining that. – Jamal Oct 25 '16 at 19:51
  • Which binary format are you trying to represent? Java uses 2's complement for integers, they're also 32 bit (not 25), you seem to be storing 25 bit unsigned integers. – Jonny Henly Oct 25 '16 at 19:52
  • Possible duplicate of [Print an integer in binary format in Java](http://stackoverflow.com/questions/5263187/print-an-integer-in-binary-format-in-java) – slartidan Oct 25 '16 at 19:54

2 Answers2

0

Probably it will be easier to use toBinaryString method from Integer class e.g:

String binaryString = Integer.toBinaryString(someIntValue);
eparvan
  • 1,639
  • 1
  • 15
  • 26
0

The code is mostly correct. But you mistyped some of the names of the variables. For example, you typed index when you actually mean indexNo. I have corrected them for you:

public void showBinaryFormat(int numbers){
    int binaryNo[] = new int[25];
    int indexNo = 0;
    while(numbers > 0){
        binaryNo[indexNo++] = numbers%2;
        numbers = numbers/2;
    }
    for(int i = indexNo-1;i >= 0;i--){
        System.out.print(binaryNo[i]);
    }
}

Note that your code can only 25 bit unsigned integers.

Alternative solutions, well, you can add static to the showBinaryFormat method so you can call it more conveniently. Or just be lazy and use Integer.toBinaryString() instead.

Sweeper
  • 213,210
  • 22
  • 193
  • 313