1

So my teacher is having us work with for loops and one of our assignments is to make a for loop that will change any base 2 number to base 10. I'll post what I have done so far. I'm only in AP Computer Science to the code will look amateurish.

    public long getBaseTen( )
{
    long ten=0;
    for (int i = 0; i < binary.length()-1; i++)
    {
        if (binary.charAt(binary.length()-i-1) == '0');
            ten += 0;
        if (binary.charAt(binary.length()-i-1) == '1');
            ten += Math.pow(2, i);
    }
    return ten;
}

binary is a string variable that contains the base 2 number earlier specified by the user. I need to convert this base 2 string into base 10 and store that number into long ten. Right now whenever I call this method, I always get the same number depending on the length of the string. If the string is 2 letters long, it will always return a 1, if it's 3 letters long, it will always return a 3, if it's 4 letters long, it will always return a 7 and so on. Help would be very much appreciated.

3
  • Notice, i < binary.length()-1 it's wrong too. Check my answer below. Jan 16, 2014 at 20:50
  • the Math.pow(2,i) could be replaced with a variable long factor starting as 1, then being multiplied by 2 for each turn (not only the turns where char=='1').
    – claj
    Feb 1, 2018 at 12:41
  • Oh, there's a read method in the Integer class Integer.parseInt(urString,2) where 2 is the radix.
    – claj
    Feb 1, 2018 at 12:58

4 Answers 4

3

The problem in your code is that your if-statements closes to early:

if (binary.charAt(binary.length()-i-1) == '0');
           ten += 0;

should be

if (binary.charAt(binary.length()-i-1) == '0'){
           ten += 0;
}

There are, of course, some other things that could be done differently, but you'll figure that out along the way.

4
  • 1
    I can't believe I made such an obvious mistake and didn't catch it. That's embarrassing. Thanks to both of you! Jan 16, 2014 at 20:31
  • No worry. The C-style syntaxen of if-statements are far from trivial.
    – claj
    Jan 16, 2014 at 20:35
  • This is another reason why I advocate always using {} with if statements, with the opening { on the same line as the if -- makes such a mistake a little more obvous. (An editor that automatically indents will help too, as it would not have indented the ten += ... statements, so they wouldn't have looked right.)
    – Hot Licks
    Jan 16, 2014 at 20:44
  • 1
    (About half of the "art" of programming is avoiding stupid mistakes. The other half is finding the ones you didn't avoid.)
    – Hot Licks
    Jan 16, 2014 at 20:46
0

Remove the semicolons at the end of the "if" lines. They shouldn't be there; they're being interpreted as

 if(whatever)
     ; // null statement -- do nothing

(You could also add braces around the block of code controlled by the if, but that's optional when you're just trying to control a single statement. Some folks always use the braces, but that decision is very much a matter of coding style.)

2
  • You're right, that would work as well, I chosed to tell the more general way of embracing the statement(s) with { }.
    – claj
    Jan 16, 2014 at 20:27
  • Updated to clarify. The semicolon is the problem. The braces help clarify and prevent future errors, but are optional and since the OP wasn't using them I saw no need to add them. De gustibus...
    – keshlam
    Jan 16, 2014 at 20:30
0

A string of 0's and 1's follows a power of two rule. The right most value is 2^0 then 2^1 2^2 and so on the exponent doubling each time. Knowing this you can make an easy for loop to do the conversion.

For example:

 int tens;
 for(int i=1;i<binary.length()-1; i++){
    tens += Math.pow(2,binary.length-i);
 }

For example if the binary number is 0101 we know this to be 5. We will start at binary.length()-1 which would be 3, perfect since the right most value 0 is represented by 2^3. The second number 1 is represented by 2^2 which if you notice binary.length()-i at this point is 2.

If you follow the logic this should work, may need a small syntax fix.

1
  • yes, this (probably) works and is compact, which is very good. It may not be very quick and could be even be wrong in some circumstances. </say no more teacher mode>
    – claj
    Jan 16, 2014 at 20:33
0

You are not need use one if statement, you can use this

public long getBaseTen()
{
    long ten=0;
    for (int i = 0; i < binary.length(); i++)
    {
        if (binary.charAt(i) == '1')
            ten += Math.pow(2,  binary.length()-i-1);
    }
    return ten;
}

Notice, i < binary.length()-1 it's wrong too.

You can see how it works here http://ideone.com/swzitQ

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.