New Reply

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

 
Share Thread Thread Tools
Jul18-12, 09:10 AM   #1
 

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


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! :/
PhysOrg.com
PhysOrg
science news on PhysOrg.com

>> Intel's Haswell to extend battery life, set for Taipei launch
>> Galaxies fed by funnels of fuel
>> The better to see you with: Scientists build record-setting metamaterial flat lens
Jul18-12, 09:23 AM   #2
 
Mentor
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.
Quote by Mehwish-S View Post
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?
Jul18-12, 09:27 AM   #3
 
Thank you so much
Im new to this so I dont know how to use it much :S
No, I couldnt enter the Binary Number because im using netbeans
How is it possible to enter the number? Do i just type it in next to "Enter Binary Number" ?
New Reply

Tags
binary, conversion, decimal, java, loop
Thread Tools


Similar Threads for: Write a Java program to convert binary numbers to decimal numbers.
Thread Forum Replies
Convert decimal to binary/octal/hex in assembly? Engineering, Comp Sci, & Technology Homework 1
Convert decimal base to binary Engineering, Comp Sci, & Technology Homework 6
How do I convert signed decimal to binary Engineering, Comp Sci, & Technology Homework 7
C program (string - binary to decimal conversion) Engineering, Comp Sci, & Technology Homework 4
binary to decimal confusion!!! signed numbers! Engineering, Comp Sci, & Technology Homework 1