- #1

- 95

- 0

## Main Question or Discussion Point

Question:

Write a program that reads a four-bit binary number from the keyboard as a string and then converts it into decimal. For example if the input is 1100 the output should be 12.

Attempt:

This code is incomplete as I am not sure how to go about it.

I think, I know how to convert binary number to decimal. You just multiply each number by the base of 2 depending on which place the number holds. Then add up the numbers to get a total ?

Basically, I am having trouble assigning String to number and then splitting it into substrings. How do I do the calculation with each substrings? I'll appreciate if someone could help me out with the code. Thanks.

Write a program that reads a four-bit binary number from the keyboard as a string and then converts it into decimal. For example if the input is 1100 the output should be 12.

Attempt:

Code:

```
import java.util.Scanner;
public class run
{
private static final String number = null;
public static void main(String[] args)
{
int number;
String value;
Scanner keyboard = new Scanner (System.in);
System.out.print("Enter Number");
number=keyboard.nextInt();
value=""+number;// concatenated integer number to string value.
value=value.substring(0,1);// How do I convert this substring to a value of bit?
}
}
```

I think, I know how to convert binary number to decimal. You just multiply each number by the base of 2 depending on which place the number holds. Then add up the numbers to get a total ?

Basically, I am having trouble assigning String to number and then splitting it into substrings. How do I do the calculation with each substrings? I'll appreciate if someone could help me out with the code. Thanks.

Last edited: