-1

I'm almost done with implementing a code that allows the user to input a decimal number into binary and then have the binary number reversed and then converted back to decimal.

I solved everything except for the Binary to decimal part. It keeps giving me the same number over and over again no matter what I type. I don't understand why. Basically, why is the last part of my code wrong? I'm having trouble with finding the length of the array/string and then have it multiply by 2^n etc...

Euridice01
  • 2,510
  • 11
  • 44
  • 76

2 Answers2

0

number will always be zero or less according to your do-while loop condition: while(number>0);

this causes your variable s (please pick better variable names...) to always be "0".

i think you can figure out the rest.

Alex Lynch
  • 951
  • 5
  • 11
  • Can you give me a bigger hint? I'm still confused? Even if I remove the do-while, it's still the same error – Euridice01 Feb 09 '13 at 19:26
  • @Euridice01 You say: ` It keeps giving me the same number over and over again no matter what I type.` Your variable `s` will always be equal to "0". That is why you get the same value every time. Please apply some basic debugging yourself to see if you can make the program work as you expect it to. – Alex Lynch Feb 09 '13 at 20:11
  • I'm sorry. I've gone over this for over 2 hours and I'm still confused. The number in the do-while will always be greater than 0? What's wrong with that? Why is S zero? – Euridice01 Feb 09 '13 at 20:32
0

There are two issues in this code.

1) You didn't store the result of decimal-to-binary convertation. You should introduce new String variable to store string with reversed binary value.

2) Your binary-to-decimal algorithm is incorrect. s.charAt(i) returns char value but you need double value to use it in calculation. Math.pow(2, (s.length() - i - 1)) is also incorrect - as I understand it's for non-reversed binary values.

Fixed version of code should looks like:

public static void main(String[] args) {
    int a[] = {0, 1};

    int number;
    int remainder;
    String binary = "";

    Scanner in = new Scanner(System.in);

    System.out.print("Enter Decimal Number: ");
    number = Integer.parseInt( in.next());

    System.out.print("Binary Number in Reverse: ");
    do {
        remainder=number%2;
        if(remainder > 0){
            binary += a[1];
            //System.out.print(a[1]);
        }
        else{
            binary += a[0];
            //System.out.print(a[0]);
        }
        number=number / 2;
    } while(number>0);

    System.out.print(binary);

    System.out.print(" \nDecimal number: ");
    //String s = Integer.toString(number);
    double result = 0;
    for (int i = 0; i < binary.length(); i++)
       result = result + Double.parseDouble(binary.substring(i, i + 1)) * Math.pow(2, i);
    System.out.print(result);
}
siarheib
  • 159
  • 1
  • 8
  • Thank you. Actually, I wanted the reverse binary decimal output but I can do that myself. Thank you so much!! – Euridice01 Feb 09 '13 at 22:28