Hey everyone, as I am totally new to Java programming, I am currently trying to convert a Binary number to Decimal. The task was to :
Use a loop to read (charAt()) each digit (0/1 char) in the input string, scanning from right to left;
Use the loop to build the required powers of 2;
Use a conditional statement to deal with 0 and 1 separately;
Debug using simple input, e.g. 1, 10, 101, and print intermediate values in the loop.
Use your program to find the decimal value of the following binary number:
1001010101011010111001011101010101010101
public class BinarytoDecimal {
public static void main (String[]args) {
String digit = "1001010101011010111001011101010101010101";
String output = "";
for (int i= 0; i <digit.length(); i++) { // walk over all digits
output*= 2; // prepare for another digit, see above
output+= // digit at location i
System.out.println(output);
}
}
And with my very beginner knowledge of Java, I wrote the above code which still does not work. I would be delighted if anyone can help me with fixing the code?