title: Decimal to binary assignment in Java
body: <p>Hi I have an assignment where I have to convert a variable 'binary' (which is a random binary (11001 for example)) and print it. </p>
  
  <p>It's hard because there is no potency feature built in Java</p>
  
  <p>Example: 1010 = 1*2^3 + 0*2^2 + 1*2^1 + 0*2^0 = 8 + 0 + 2 + 0 = 10</p>
  
  <p>What's wrong with my code?</p>
  
  <pre><code>int a = 1;
  int b = 0;
  for (int i=binary.length()-1;i&gt;0;i--){
      if (binary.charAt(binary.length()-1) == '1'){
          b += 1;
      if (binary.charAt(i) == '1'){
          for (int j=binary.length()-1;j&gt;0;i--){
              a = a*2;
          }
      }
      }
  }
  System.out.println(a+b);
  </code></pre>
  
  <p>Currently it only prints "1"</p>