# Thread: anyone know's how to program a conversion of binary-decimal , decimal-binary

1. Member
Join Date
Aug 2011
Posts
28
Rep Power
0

## anyone know's how to program a conversion of binary-decimal , decimal-binary

import java.lang.*;
import java.io.*;
public class DecimalToBinary{
public static void main(String args[]) throws IOException{
System.out.println("Enter the decimal value:");
int i = Integer.parseInt(hex);
String by = Integer.toBinaryString(i);
System.out.println("Binary: " + by);
}
}

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

public class BinaryToDecimal{
public static void main(String[] args) throws IOException{
System.out.print("Enter the Binary value: ");
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.exit(0);
}
}
int i= Integer.parseInt(str,2);
System.out.println("Decimal:="+ i);
}
}

IF YOU PUT DECIMAL NUMBER LIKE 25.5 IT WILL BE ERROR .... WHO CAN FIX IT??

2. You abandoned the last thread you started when asked to post the errors. Why should we expect anything better here?

db

3. IF YOU PUT DECIMAL NUMBER LIKE 25.5 IT WILL BE ERROR
Do you want it to be an error? Or do you want your code to take that number and return a value?

Have you tried using the Double class's methods in addition to the Integer class method?

4. The reason that you are getting an exception, which looks like this:

\$ java DecimalToBinary
Enter the decimal value:
25.5
Exception in thread "main" java.lang.NumberFormatException: For input string: "25.5"
at java.lang.NumberFormatException.forInputString(Num berFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:481)
at java.lang.Integer.parseInt(Integer.java:514)
at DecimalToBinary.main(DecimalToBinary.java:8)

is because you are using Integer.parseInt(String). This expects a non-decimal number in a String, but your input is a decimal number, because it contains a decimal point. This being true results in a NumberFormatException. Integer expects numbers like 0, 25, 362, and 10304. There is are two alternate classes for decimal numbers, Float and Double, which have different precisions - Float is 32 bits wide and Double is 64 bits wide. I would generally suggest you use Double if you expect the need for precision to be high. Your example of 25.5 does not need 64 bits of precision however, so for this specific case and similar cases you could use Float. Float has a similar method to Integer's parseInt(String), which is Float.parseFloat(String). Double likewise has a similar method: Double.parseDouble(String). Change out all of your usage of Integer in the DecimalToBinary class and this will solve your parsing problem.

After that however you will likely have more issues because Float and Double do not have toBinaryString methods, so your program will fail to compile. The reason for this is that Java internalizes the representation of Float and Double values so as to retain consistency across all platforms (operating systems and system architectures), so that outputting a binary representation of a Float or Double may not be compatible with the system that is being used. Instead, you may 1) choose to only accept number inputs that contain no decimals, 2) remove the decimals and everything after the decimal, or 3) write your own implementation of a toBinaryString method for Floats or Doubles.

To all of those who failed to help this individual in answering his question, because he didn't provide the error that was coming out based on the input that he provided - it took me all of two minutes to copy his code into a file on my computer, compile it, and test his input to get the above output. More effort would likely be appreciated by those who are responding to questions on this forum, I'm sure.

5. More effort would likely be appreciated by
everyone involved.
If you want to do the OPs work for him that is your prerogative. Although some would rather you didn't.
Part of learning how to program is learning how to present your problems to others. Logical thought.
There are lots of OPs that dump their code at the first problem and wait for someone else to find their problem and fix it. What are they learning if someone here on a forum does all that?
The object here is to teach programming not to provide answers.

6. I agree Norm. Thanks for the explanation. When it comes to debugging however, I feel that a little bit more help would be appreciated by beginners. Debugging isn't exactly cut and clean, and if you're new to stack traces, then they can be very overwhelming. I hope that clarifies a little more what I was referring to. I apologize for my blanket comment. And I completely agree that we shouldn't do all of the work, but maybe we should just hint a little as to where to begin. Thanks again.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•