Joan - 5 months ago 16

Java Question

I made a java console program that prints the decimal value of the binary value inputted. Now, I have a problem with my program that

for e.g. output:

Enter input = 10011101 then the binary value is 3534200 instead of 157

After surfing the internet for the formula in converting binary value to decimal, this is the reference I took for making this program.

1*2^7 + 0*2^6 + 0*2^5 + 1*2^4 + 1*2^3 + 1*2^2 + 0*2^1 + 1*2^0 = 157.

I tried the long version in making this program, in using for loop... I guess that's a dumb challenge? haha!

here's the code(w/ comments!):

`byte binary[] = new byte[127]; //declared a byte array for input value`

int power = 2, formula = 0; //declared the power and the formula as int

System.out.print("Enter Binary: "); //prints "Enter binary:"

System.in.read(binary); //inserts the input in the binary array

Integer bin = Integer.parseInt(new String(binary).trim()); //converts the input value to int for another conversion

String b = Integer.toString(bin); //converts the int bin to string

Integer num = Integer.parseInt(new String(b.substring(b.length() - 1).trim()));

//num variable gets the value of last string (should be anyway)

System.out.println("The Decimal Value of " + bin + " is ");

for(int i = b.length(); i > 0; i--){

for(int a = i; a > 0; a--){ //the condition

power = power*2; //as the loop goes, if 2^3 then it should be 8

}

formula = formula + (num * (power)); //as the given formula above, this is what I did

System.out.println("power: " + power);//if you want reference, I left it here

System.out.println("formula: " +formula);

System.out.println("num: " + num);

num = Integer.parseInt(new String(b.substring(i - 1).trim())); //uhm dunno how to describe this, but you'll see

power = 2;

}

System.out.print(formula);

}

}

ever since I started using java, this is the only thing that I know. (refer to my last question since sep 4)

please help :(

Answer

You have a number of problems:

1) `Integer.parseInt(new String(binary).trim())`

does not do what you think it does. Therefore, your num is wrong.

2) You calculate your power wrong.

3) A general advice, put some empty lines to separate your code into small blocks that make sense. It will be easier on the eyes.

The fixed program should look like this:

```
public static void main(String[] args) throws IOException {
byte binary[] = new byte[127]; //declared a byte array for input value
int power = 2, formula = 0; //declared the power and the formula as int
System.out.print("Enter Binary: "); //prints "Enter binary:"
System.in.read(binary); //inserts the input in the binary array
Integer bin = Integer.parseInt(new String(binary).trim()); //converts the input value to int for another conversion
String b = Integer.toString(bin); //converts the int bin to string
Integer num; //num variable gets the value of last string (should be anyway)
System.out.println("The Decimal Value of " + bin + " is ");
for(int i = b.length(); i > 0; i--){
power = 1;
num = Integer.parseInt(Character.toString(b.charAt(i-1)));
for(int a = i; a < b.length(); a++){ //the condition
power = power*2; //as the loop goes, if 2^3 then it should be 8
}
formula = formula + (num * (power)); //as the given formula above, this is what I did
System.out.println("power: " + power);//if you want reference, I left it here
System.out.println("formula: " +formula);
System.out.println("num: " + num);
}
System.out.print(formula);
}
```