Results 1 to 6 of 6
- 08-10-2011, 11:19 AM #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{
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the decimal value:");
String hex = bf.readLine();
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{
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);
}
}
IF YOU PUT DECIMAL NUMBER LIKE 25.5 IT WILL BE ERROR .... WHO CAN FIX IT??
- 08-10-2011, 11:28 AM #2
You abandoned the last thread you started when asked to post the errors. Why should we expect anything better here?
db
- 08-10-2011, 03:29 PM #3IF YOU PUT DECIMAL NUMBER LIKE 25.5 IT WILL BE ERROR
Have you tried using the Double class's methods in addition to the Integer class method?
- 08-10-2011, 08:15 PM #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.
- 08-10-2011, 08:45 PM #5More effort would likely be appreciated by
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.
- 08-25-2011, 08:32 PM #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.
Similar Threads
-
Decimal to binary, octal to decimal
By matejm1994 in forum New To JavaReplies: 3Last Post: 12-26-2010, 10:59 AM -
Binary-to-Decimal Conversion (Precision Problem)
By shadowfax57 in forum New To JavaReplies: 4Last Post: 09-07-2010, 10:00 AM -
Convert Decimal To Binary
By aspire007 in forum New To JavaReplies: 8Last Post: 08-06-2010, 08:32 AM -
how to convert decimal value into 8-bit binary value
By tOpach in forum New To JavaReplies: 4Last Post: 10-26-2009, 11:17 PM -
Eclipse- Decimal to binary
By queen_vee in forum New To JavaReplies: 1Last Post: 02-24-2009, 03:17 PM
Bookmarks