Java Convert 4-Bit Binary Number to Decimal in Java

  • Thread starter Thread starter J.live
  • Start date Start date
  • Tags Tags
    Binary
Click For Summary
To convert a four-bit binary number to decimal in Java, the program should read the input as a string instead of an integer. The conversion involves checking each character of the string using `charAt`, converting it to its numeric value by subtracting '0', and multiplying it by the appropriate power of 2 based on its position. Although loops are typically used for such tasks, the discussion suggests manually calculating each bit's contribution for a fixed four-bit input. The final output should correctly reflect the decimal equivalent of the binary input, ensuring that the calculations are based on the correct character values. Understanding these concepts is crucial for successfully implementing the conversion logic.
  • #31
hey J.live
I am currently working on this problem as well. I've tried your code but i keep getting an error for the code line:

number=keyboard.nextLine();
number=keyboard.nextLine();

do you know why by any chance?
 
Technology news on Phys.org
  • #32
Mark44 said:
Here's the code with the extra input line removed, and using the += operator, plus the addition of some white space for better readability.
Code:
int value;
String number;
				
		 	     
System.out.print("Enter Number ");
number = keyboard.nextLine();
		 		
value=0;
value += (number.charAt(0) - '0') * 8;
value += (number.charAt(1) - '0') * 4;
value += (number.charAt(2) - '0') * 2;
value += (number.charAt(3) - '0') * 1;

System.out.println(value);
hey Mark44
I am currently working on this problem as well. I've tried your code but i keep getting an error for the code line:

number=keyboard.nextLine();
number=keyboard.nextLine();

do you know why by any chance?
 
  • #33
Why do you have two lines there?

Show us your code and I'm sure we can figure it out.
 
  • #34
my codes are:


import java.util.Scanner;

public class project11p117 {
Scanner input = new Scanner(System.in);
public static void main(String [] args){

int value;
String number;


System.out.print("Enter Number");
number=keyboard.nextLine();


value=0;
value= value+(number.charAt(0)-'0')*8;
value=value+(number.charAt(1)-'0')*4;
value=value+(number.charAt(2)-'0')*2;
value=value+(number.charAt(3)-'0')*1;

System.out.println(value);

}
}
 
  • #35
my codes are:import java.util.Scanner;

public class project11p117 {
Scanner input = new Scanner(System.in);
public static void main(String [] args){

int value;
String number;System.out.print("Enter Number");
number=keyboard.nextLine();value=0;
value= value+(number.charAt(0)-'0')*8;
value=value+(number.charAt(1)-'0')*4;
value=value+(number.charAt(2)-'0')*2;
value=value+(number.charAt(3)-'0')*1;

System.out.println(value);

}
}
 

Similar threads

  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
8
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 0 ·
Replies
0
Views
516
  • · Replies 3 ·
Replies
3
Views
10K
Replies
1
Views
2K
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K