-2

I'm supposed to create a program that takes user-entered decimal number and converts it to binary using an inner and outer for or while loop.

For some reason, whenever I enter a positive integer the program just does nothing.

My code:

public class Dec2Bin
{
    public static void main (String[] args)
    {
        System.out.println("Welcome to Decimal Number to Binary Converter");
        System.out.println("");

        Scanner s = new Scanner(System.in);
        while (true)
        {
            String binary = "";
            System.out.print("Enter a decimal number (-1 to end)");
            int input = s.nextInt();

            if (input <= 0)
            {
                System.out.println("Goodbye!");
                break;
            }

            int result = input;

            while (result >= 0)
            {
                result = result / 2;
                binary = (result % 2) + binary;
            }
            System.out.println("Binary number is " + binary);
        }
    }
}

It should be prepending the remainder from result%2 to the binary string each time and that string should show the converted binary number. It does work as it's supposed to if I enter a negative number, displaying the "Goodbye!" message. Not sure where I went wrong.

sam
  • 151
  • 1
  • 3
  • 15
  • ALWAYS use `if{}else{}` just to make sure your intention is communicated that there are only 2 branches of logic that should be executed. –  Oct 14 '14 at 19:10
  • Just a note, it will exit if you enter anything `0` or below, while your prompt says only entering `-1` will exit. – forgivenson Oct 14 '14 at 19:12
  • here is a hint, the step debugger in your IDE will show you EXACTLY why this isn't working as you intended –  Oct 14 '14 at 19:13
  • thanks Jarrod, I added that. Still not working when I enter a positive integer, however. – sam Oct 14 '14 at 19:13
  • did you step debug through it? that is what the debugger is there for, debugging! –  Oct 14 '14 at 19:14
  • when i debug in eclipse it just does the same thing as when I run it normally. I enter a number, and then nothing. – sam Oct 14 '14 at 19:18
  • The debugger isn't going to work itself, by using a debugger one adds breakpoints points to the program and uses the step commands to slowly execute the program and see what happens line by line, and what values your variables have. If you do that, you would be able to see that the program does not do nothing, but actually executes your loop over and over, which you would lead you to think that you have an infinite loop bug. – NESPowerGlove Oct 14 '14 at 19:21

3 Answers3

2

Your problem is the infinite loop caused by

while (result >= 0)
{
    result = result / 2;
    binary = (result % 2) + binary;
}

Change your

while (result >= 0)

to

while (result > 0)

and test as your current code doesn't work (for example decimal 1 should return 1 instead of 0)

gtgaxiola
  • 8,840
  • 5
  • 41
  • 60
  • Wow, you were right. Such a simple fix that I totally missed. Thank you. – sam Oct 14 '14 at 19:15
  • Hmm and you're also right as the binary numbers are not coming out accurately. But the infinite loop is fixed, so that's good. – sam Oct 14 '14 at 19:17
  • also in the while loop exchange the two line binary = (result % 2) + binary; result = result / 2; – Aadil Ahmad Oct 14 '14 at 19:19
2

Change your code to this:

        Scanner s = new Scanner(System.in);
        while (true)
        {
            // String binary = ""; --> do not need this
            System.out.print("Enter a decimal number (-1 to end)");
            int input = s.nextInt();

            if (input < 0) {
                System.out.println("Goodbye!");
                break;
            } else {
            System.out.print("Binary number is ");

            int binary[] = new int[8];  // ------> assume 8-bit, you can make it 16, 32, or 64
            int count = 0;
            while( input != 0 ) {
                binary[count++] = input % 2;//--> this code is equivalent to: 
                input = input / 2;          //    binary[count] = input % 2;
            }                               //    count++
            for (int i = 7; i >= 0; i--) {  // printing the binary array backwards
                System.out.print(binary[i]);
            }
        }
            System.out.print("\n\n");
        }
Edwin
  • 825
  • 1
  • 6
  • 15
0

Change the order of this 2 commands.

Keep it in this order

binary = (result % 2) + binary; result = result / 2;

Full code

import java.util.Scanner;

public class DecimalToBinary {

    public static void main (String[] args)
    {
        System.out.println("Welcome to Decimal Number to Binary Converter");
        System.out.println("");

        Scanner s = new Scanner(System.in);
        while (true)
        {
            String binary = "";
            System.out.print("Enter a decimal number (-1 to end)");
            int input = s.nextInt();

            if (input <= 0)
            {
                System.out.println("Goodbye!");
                break;
            }

            int result = input;

            while (result > 0)
            {
                binary = (result % 2) + binary;
                result = result / 2;
            }
            System.out.println("Binary number is " + binary);
        }
    }
}
KingJulien
  • 293
  • 3
  • 9