Convert 4-Bit Binary Number to Decimal in Java

  • Context: Java 
  • Thread starter Thread starter J.live
  • Start date Start date
  • Tags Tags
    Binary
Click For Summary

Discussion Overview

The discussion revolves around writing a Java program to convert a four-bit binary number input as a string into its decimal equivalent. Participants explore various coding approaches, address specific requirements, and clarify programming concepts related to string manipulation and binary conversion.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant suggests that the input should be handled as a string rather than an integer, emphasizing the need to read a four-bit binary number correctly.
  • Another participant proposes using a loop to process the string from the back to the front, although there is contention about the use of loops in the assignment.
  • Some participants discuss using bit shifting as an alternative to exponentiation for converting binary to decimal.
  • There is a suggestion to use the charAt method to read individual bits instead of substring methods, with a focus on checking each bit's value.
  • One participant expresses confusion about the requirement to avoid loops entirely, questioning how to implement charAt without looping.
  • Another participant mentions the Integer class's parseInt method as a simpler solution, but acknowledges that it may not align with the teacher's intentions for the assignment.
  • Some participants discuss the feasibility of handling a fixed number of bits without loops, suggesting that for four bits, a loop may not be strictly necessary.
  • There are clarifications on how to multiply the base for each bit and the necessity of using conditional statements to check the character values.

Areas of Agreement / Disagreement

Participants express differing views on the necessity and appropriateness of using loops for the assignment. While some suggest that a loop is essential for general cases, others argue that for a fixed four-bit input, it may be possible to avoid loops. The discussion remains unresolved regarding the best approach to take under the given constraints.

Contextual Notes

Participants highlight limitations in the assignment's requirements, particularly the restriction against using loops, which complicates the implementation of the binary to decimal conversion. There is also mention of the need to validate input length and character content.

  • #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 1 ·
Replies
1
Views
969
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
10K
Replies
4
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K