Results 1 to 6 of 6
  1. #1
    irnie1994 is offline Member
    Join Date
    Aug 2011
    Posts
    28
    Rep Power
    0

    Default 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??

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default

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

    db

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,581
    Rep Power
    25

    Default

    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. #4
    Join Date
    Aug 2011
    Location
    salt Lake City, Utah
    Posts
    10
    Rep Power
    0

    Default

    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. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,581
    Rep Power
    25

    Default

    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. #6
    Join Date
    Aug 2011
    Location
    salt Lake City, Utah
    Posts
    10
    Rep Power
    0

    Default

    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

  1. Decimal to binary, octal to decimal
    By matejm1994 in forum New To Java
    Replies: 3
    Last Post: 12-26-2010, 09:59 AM
  2. Binary-to-Decimal Conversion (Precision Problem)
    By shadowfax57 in forum New To Java
    Replies: 4
    Last Post: 09-07-2010, 09:00 AM
  3. Convert Decimal To Binary
    By aspire007 in forum New To Java
    Replies: 8
    Last Post: 08-06-2010, 07:32 AM
  4. Replies: 4
    Last Post: 10-26-2009, 10:17 PM
  5. Eclipse- Decimal to binary
    By queen_vee in forum New To Java
    Replies: 1
    Last Post: 02-24-2009, 02:17 PM

Posting Permissions

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