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

In summary: When you post code, put a [code] tag at the top and a [/code] tag at the bottom. This preserves your indentation, if any, and makes your code easier to read.
  • #1
Mehwish-S
2
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
  • #2
When you post code, put a [code] tag at the top and a [/code] 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:
  • #3
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" ?
 

1. How do you write a Java program to convert binary numbers to decimal numbers?

To write a Java program to convert binary numbers to decimal numbers, you will need to use the Integer.parseInt() method to convert the binary number into an integer. Then, you can use the Integer.toBinaryString() method to convert the integer into a decimal number.

2. What are the steps to convert a binary number to a decimal number in Java?

The steps to convert a binary number to a decimal number in Java are as follows:

  1. Store the binary number as a string.
  2. Convert the binary number into an integer using Integer.parseInt().
  3. Use the Integer.toBinaryString() method to convert the integer into a decimal number.
  4. Print the decimal number.

3. How do you handle invalid input in a Java program to convert binary numbers to decimal numbers?

To handle invalid input in a Java program, you can use a try-catch block to catch any exceptions that may occur when converting the binary number to an integer. You can also add a condition to check if the input is a valid binary number before attempting to convert it.

4. Can you convert a decimal number to a binary number in Java?

Yes, you can use the Integer.toBinaryString() method to convert a decimal number to a binary number in Java. This method takes in an integer as a parameter and returns the binary representation of that number as a string.

5. How can you optimize a Java program to convert binary numbers to decimal numbers?

One way to optimize a Java program to convert binary numbers to decimal numbers is to use a loop to iterate through each digit of the binary number instead of converting the entire number at once. This can help reduce the time and memory complexity of the program.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
18
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
4K
  • Programming and Computer Science
Replies
16
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
22
Views
730
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
1K
Back
Top