Search code examples
javadecimal

Making a binary to decimal converter


EDIT: Since there are claims that this is a duplicate question, I will state my stance on why it is not.

  1. The other question had posed a problem regarding the conversion of a string input to an integer (the answer deemed correct). This is irrelevant here, as the problem in my case is one of logic.

  2. The solutions on the other question cannot correct the problem this question presents (finding the syntactical "charAt" error).

  3. Frankly, the context of the other question is beyond the scope of my knowledge in Java.

I have successfully created a program that converts binary numbers to decimal numbers.

import java.util.Scanner;
public class ProblemThree // to convert binary to decimal
{
  public static void main (String[]args)
  {
    Scanner scan = new Scanner (System.in);
    System.out.print("Enter binary");
    int binary = scan.nextInt();
    int answer = 0;
    int length = String.valueOf(binary).length();
    int[] number = new int[length];
    int[] powerTwo = new int[length];
    
    // To list every digit separately //
    for (int n = 0; n <= length - 1; n++)
    {
      number[n] = (int) (binary / Math.pow(10, n)) % 10;
    }
    
    // To set the values in powerTwo to 2 //
    for (int n = 0; n <= length - 1; n++)
    {
      powerTwo[n] = 2;
    }
    
    // To update the values in powerTwo //
    for (int n = 0; n <= length - 1; n++)
    {
      if (n == 0)
        powerTwo[0] = 1;
      else if (n == 1)
        powerTwo[1] = 2;
      else
        powerTwo[n] *=  powerTwo[n-1];
    }
    
    // To add if current value is 1 //
    for (int n = 0; n <= length - 1; n++)
    {
      if (number[n] == 1)
        answer += powerTwo[n];
    }
    System.out.println(powerTwo[0]); // for testing
    System.out.println(number[0]); // for testing
    System.out.println("The decimal version of " + binary + " is " + answer + ".");
  }
}

Seeing how some of the steps were redundant, I tried to simplify the code.

import java.util.Scanner;
public class ProblemThreeTestFunction
{
  public static void main (String[]args)
  {
    Scanner scan = new Scanner (System.in);
    System.out.print("Enter binary");
    int binary = scan.nextInt();
    int answer = 0;
    int length = String.valueOf(binary).length();
    String number = String.valueOf(binary);
    
    for (int n = 0; n <= length - 1; n++)
    {
      if (number.charAt(n) == 1)
        answer += (int)Math.pow(2, n);
    }
    System.out.println("The decimal version of " + binary + " is " + answer + ".");
  }
}

I actually have no idea why the simplified code logic does not work. It uses the same concept as my original.


Solution

  • You need to test against the character '1':

    if (number.charAt(n) == '1')
    

    not the number 1 that you are currently doing:

    if (number.charAt(n) == 1) // WRONG!
    

    Also, since charAt counts from the left, and binary expansion counts from the right, you need to reverse the sense. Change

    Math.pow(2, n)
    

    to

    Math.pow(2, length - 1 - n)