0

I am a first year Computer Science student. This question has been asked many times and I have gone through those. But I still could not find where I need to fix in my current code. I have written the code to convert Decimal to Binary. The following is the sample input and output.

Sample Input

4
101
1111
00110
111111

Sample Output

5
15
6
63

I understand the concept and binary conversion. However I am not able to enter binary value for the specified number and am getting incorrect output. I cannot use Integer.parseInt. Below is the rough conversion workout from binary to decimal.

Binary to Decimal 
    1       0       1       0 -binary
    3       2       1       0 -power
    2       2       2       2 -base
    1*2^3 + 0*2^2 + 1*2^1 + 0*2^0
    8     + 0     + 2     + 0     = 10

Code

public class No2_VonNeumanLovesBinary {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        int numTotal, binaryNum, decimalNum = 0, remainder;
        numTotal = s.nextInt();
        for(int i = 0 ; i <= numTotal; i++){
        // This is to get binaryNum input. However I am not getting the expected result.
            binaryNum = s.nextInt();
            while(binaryNum != 0){
                remainder = binaryNum % 10;
                decimalNum = decimalNum + (remainder * i);
                i = i * 2;
                binaryNum = binaryNum / 10;
            }
            System.out.println(decimalNum);
        }
    }
}

Thanks!

Suス
  • 1
  • 3
  • In `(remainder + i)` and `i = i * 2`, you should replace `i` with an entirely other variable (let's call it `powerOfTwo`, because that's what it is...), which you will set to 1 just before the `while` loop begins. – Kevin Anderson Jun 12 '20 at 04:43
  • @KevinAnderson Do you mean use Integer.parseInt(binaryNum, 2) to change to base 2? I do not want to use it. I want to learn how to use mathematical expression way of coding first before using this Integer.parseInt method. Thanks. – Suス Jun 12 '20 at 04:59
  • @KevinAnderson By using that method, it is relatively straight forward. for (int i = 1; i <= N; i++) { String binaryNumber = sc.next(); System.out.println(Integer.parseInt(binaryNumber, 2)); } – Suス Jun 12 '20 at 05:00
  • No, you've got the right idea: isolate the binary digit, multiply by the binary place value, add to the decimal number, and advance to the next binary place value. The problem is that `i` is the wrong thing to use for the binary place value: it's already being used as an input counter. You need a completely different variable. – Kevin Anderson Jun 12 '20 at 05:10

1 Answers1

1

Two things to fix. Use variable other than i inside while loop. Reset decimalNum to 0 after printing the value. i.e,

public static void main(String[] args) {
    Scanner s = new Scanner(System.in);
    int numTotal, binaryNum, decimalNum = 0, remainder;
    numTotal = s.nextInt();
    for(int i = 0 ; i <= numTotal; i++){
        // This is to get binaryNum input. However I am not getting the expected result.
        binaryNum = s.nextInt();
        int j = 1;
        while(binaryNum != 0){
            remainder = binaryNum % 10;
            decimalNum = decimalNum + (remainder * j);
            j = j * 2;
            binaryNum = binaryNum / 10;
        }
        System.out.println(decimalNum);
        decimalNum = 0;
    }       
}
Siva
  • 166
  • 3
  • 5
  • Thanks Siva. I appreciate your help. I see. I need to use a different variable, i.e., j. With this code, I do have compilation error. I have already initialised binaryNum to int. I am getting the error saying binaryNum may not have been initialised. Is my understanding of decimalNum = 0 correct - decimalNum set to 0 to get corresponding value for each binary value? Thanks! – Suス Jun 12 '20 at 05:34