-3

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.

5
  • Can you provide an example of incorrect output (and the corresponding input)? Commented Nov 22, 2016 at 7:44
  • Sure! Any response inputted in the simplified program gives an output of 0. Commented Nov 22, 2016 at 7:48
  • 1
    Possible duplicate of How to convert binary string value to decimal Commented Nov 22, 2016 at 7:55
  • You shouldn't be reading the binary into an int. It's an incorrect representation as it reads the number as a decimal. E.g. 101 is a hundred and one. This could lead to confusion especially when you try to debug and find that the actual binary value in that number is completely different than 101. Commented Nov 22, 2016 at 8:03
  • 1 (the integer) is not the same as '1' (the character) -- (actually '1' is 49 -- the code point of the digit 1) -- if someone still needs this, if (number.charAt(n) == '1') should do (despite not the best/easiest/... way) Commented Feb 10 at 19:34

3 Answers 3

3

Why don't you just use:

int decimalValue = Integer.parseInt(binary, 2);

But if you use this you Need to use scan.nextLine() not scan.nextInt().

This will just convert your binary number to decimal using base 10 like seen here.

Sign up to request clarification or add additional context in comments.

3 Comments

That wouldn't really help? Doesn't that just convert string to int?
That is not the problem. I do not need to convert integer to string. Sure, my code is a little inefficient, but the logic problem still remains.
@WasteD your answer was good. Now is bad. the number says in which system is the input. Numbers are stored in memory in binary anyway. We just got used to displaying them in decimal system but it doesn't matter.
1

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)

10 Comments

Was about to say that, but even with that, it won't work properly. He is first of all doing this String number = String.valueOf(binary); with his binary number, so if entering 0010 it will become 10 from parsing it. I think his logic is wrong there.
I slapped myself when I read the comment thinking I was dumb, but I still have the logic error.
@OlarAndrei - His logic is inefficient, but not wrong. The leading zeroes disappear, but that won't change the resulting value. (Binary 0010 and binary 10 both end up as decimal 2.)
This time, every input I enter outputs a "1."
Interesting development: I tested an input of 11; it outputted 3.
|
1

You are comparing with digit 1, it should be character 1 i.e. '1'.

for (int n = 0; n <= length - 1; n++) {
  if (number.charAt(n) == '1') {
    answer += (int)Math.pow(2, length - n - 1);
  }
}

Also you need to correct the exponent part.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.