Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

How to convert 2_10 in java?

package binar;

public class Convertor {

public static void main(String[] args) {
    convert2_10("101011");
}

private static void convert2_10(String string) {
    System.out.println(string.length() + " characters: " + string);
    for (int i = string.length() - 1; i >= 0; i--)
        System.out.println(string.charAt(i) + " * 2 ^ " + (string.length() - i - 1));

}

}
The code it's not finished. Please help ti finish it.

2 Answers

Thank you. And can you help me to convert from base 10 to a binary number in java?

Andrew Winkler
Andrew Winkler
37,739 Points

I see that you're trying to convert a binary number to base 10 in java.

public static void main(String[] args) {
    convert2_10("101011");


  public static int convert2_10(String string) {
    int total = 0;

    for (int i = 0; i < string.length(); i++) {
      if (string.charAt(i) == '1') {
        total += (int) Math.pow(2, string.length() - 1 - i);
      }
    }

    return total;
    System.out.println(total.length() + "characters: " + total);
  }
}

I think you're looking for something like this. Your question was very vague. Please check out this stack overflow thread if you're still confused.