Write a Java program to convert binary numbers to decimal numbers.

Click For Summary
SUMMARY

The forum discussion centers on writing a Java program to convert binary numbers to decimal numbers. The provided code utilizes a loop to read each digit of the binary input, checks for validity, and converts the binary string to a decimal integer using Integer.parseInt(str, 2). Users encountered issues with input handling in the NetBeans IDE, specifically not being able to enter the binary number after the prompt. The solution involves ensuring the program is run in a console that allows user input.

PREREQUISITES
  • Java programming fundamentals
  • Understanding of binary number representation
  • Familiarity with the BufferedReader class for input handling
  • Basic knowledge of conditional statements and loops in Java
NEXT STEPS
  • Learn how to handle user input in Java applications using Scanner instead of BufferedReader
  • Explore debugging techniques in Java to troubleshoot input issues
  • Study the conversion of binary to decimal in depth, including manual calculations
  • Investigate IDE-specific configurations for running console applications in NetBeans
USEFUL FOR

Java developers, programming students, and anyone interested in understanding binary to decimal conversion in Java applications.

Mehwish-S
Messages
2
Reaction score
0
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




I have got as far as this:

import java.lang.*;
import java.io.*;

public class BinaryToDecimal{
public static void main(String[] args) throws IOException{
BufferedReader bf= new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the Binary value: ");
String str = bf.readLine();
long num = Long.parseLong(str);
long rem;
while(num > 0){
rem = num % 10;
num = num / 10;
if(rem != 0 && rem != 1){
System.out.println("This is not a binary number.");
System.out.println("Please try once again.");
System.exit(0);
}
}
int i= Integer.parseInt(str,2);
System.out.println("Decimal:="+ i);
}
}

But when I compile and run it only says "Enter the Binary value:"
Help! :/
 
Physics news on Phys.org
When you post code, put a
Code:
 tag at the top and a
tag at the bottom. This preserves your indentation, if any, and makes your code easier to read. I have done this below.

Since you didn't do much indentation, I have done so.
Mehwish-S said:
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

I have got as far as this:
Code:
import java.lang.*;
import java.io.*;

public class BinaryToDecimal{
  public static void main(String[] args) throws IOException{
    BufferedReader bf= new BufferedReader(new InputStreamReader(System.in));
    System.out.print("Enter the Binary value: ");
    String str = bf.readLine();
    long num = Long.parseLong(str);
    long rem;
    while(num > 0){
      rem = num % 10;
      num = num / 10;
      if(rem != 0 && rem != 1){
        System.out.println("This is not a binary number.");
        System.out.println("Please try once again.");
        System.exit(0);
      }
   }
   int i= Integer.parseInt(str,2);
   System.out.println("Decimal:="+ i);
   }
}
But when I compile and run it only says "Enter the Binary value:"
Help! :/

After your program printed "Enter the Binary value:" did you enter a binary value?
 
Last edited:
Thank you so much
Im new to this so I don't know how to use it much :S
No, I couldn't enter the Binary Number because I am using netbeans
How is it possible to enter the number? Do i just type it in next to "Enter Binary Number" ?
 

Similar threads

  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 18 ·
Replies
18
Views
3K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
5K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 16 ·
Replies
16
Views
3K