Results 1 to 12 of 12
  1. #1
    manibby93 is offline Member
    Join Date
    Sep 2013
    Posts
    77
    Rep Power
    0

    Default I'm trying to convert binary to decimal & hexadecimal. Almost there??

    You can see where I'm going with this... The Binary to Decimal part is fine, but...the rest is not. When I move this part, it doesn't recognize "str"
    Java Code:
    int i= Integer.parseInt(str,2);
    System.out.println("Decimal:="+ i);
    Here is the full code:
    Java Code:
    import java.io.*;
    import java.util.*;
    public class BinToDecAndHex{
        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);
       }
    //BINARY TO HEX
     public String BinToHex(String numS){
      int a;
      int ctr=0;
      double prod=0;
      int number;
      int r;
      String hex="\0";
      for(int j=numS.length();j>0;j--){
       a=Character.getNumericValue(numS.charAt(j-1));
      prod=prod+(a*Math.pow(2,ctr));
      ctr++;
      }
      number=(int)prod;
      do{
       r=number%16;
       number=number/16;
       if(r==10)
        hex="A"+hex;
       else if(r==11)
        hex="B"+hex;
       else if(r==12)
        hex="C"+hex;
       else if(r==13)
        hex="D"+hex;
       else if(r==14)
        hex="E"+hex;
       else if(r==15)
        hex="F"+hex;
       else
        hex=r+hex;
      }while(number!=0);
      return hex;
     }
       BinToDecAndHex bth=new BinToDecAndHex();
    Scanner sc=new Scanner(System.in);
            String numS; {
      numS=sc.next();
      System.out.println(bth.BinToHex(numS));
      }
    }

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,789
    Rep Power
    7

    Default Re: I'm trying to convert binary to decimal & hexadecimal. Almost there??

    Then you must have moved it into the wrong scope.

    Why have you written the BinToHex method if you do not use it?
    How is the user supposed to try again if you terminate the program?

  3. #3
    manibby93 is offline Member
    Join Date
    Sep 2013
    Posts
    77
    Rep Power
    0

    Default Re: I'm trying to convert binary to decimal & hexadecimal. Almost there??

    I wrote it so that I could use it, lol. I don't know HOW to. Omg. Lol.
    Have mercy... I just mixed them together. I don't know. I just want to know how to make it WORK, not be questioned about why I copied & pasted something...

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,950
    Rep Power
    19

    Default Re: I'm trying to convert binary to decimal & hexadecimal. Almost there??

    You need to sort out your indenting as that's all over the place and makes it hard to see where everything actually sits.

    Also, if you have an error you really need to post the full error and highlight where in your code it's happening.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    manibby93 is offline Member
    Join Date
    Sep 2013
    Posts
    77
    Rep Power
    0

    Default Re: I'm trying to convert binary to decimal & hexadecimal. Almost there??

    There is no ERROR. It COMPILES, but the Binary to Hex part just doesn't work. The Binary to Decimal part works fine though. I don't know HOW to make it work. That's why I'm here, because I've been TRYING to get it to work for the past few days, and I have other assignments to do.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,950
    Rep Power
    19

    Default Re: I'm trying to convert binary to decimal & hexadecimal. Almost there??

    And what does "doesn't work" mean?

    The above code is (as I said) poorly indented which makes following the flow very difficult...I for one am not going to attempt to follow it.
    Added to which (as I ask in the first sentence here) you haven't actually said what's wrong.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    manibby93 is offline Member
    Join Date
    Sep 2013
    Posts
    77
    Rep Power
    0

    Default Re: I'm trying to convert binary to decimal & hexadecimal. Almost there??

    Lol. I could've sworn that I said that the binary to hex part doesn't work. Just nevermind. I found out how to do it, I think, w/o it being so wordy. By poorly indented, you mean by me not using enough spaces b/w lines or comments? I added the spaces b/w lines...

    I have ONE more question. How can I get the two println statements to show at the same time? Right now, "Enter the Binary value to convert to Decimal:" shows first, then the next one. It may be an amateur question TO YOU, but I'm just trying to pass this class for credits... & I know little to nothing.

    Java Code:
    import java.io.*;
    
    public class BinToDecAndHex{
        public static void main(String[] args) throws IOException{
            BufferedReader bf= new BufferedReader(new InputStreamReader(System.in));
    
            System.out.print("Enter the Binary value to convert to Decimal: ");
            String str = bf.readLine();
            System.out.print("Enter the Binary value to convert to Hexadecimal: ");
            String number = bf.readLine();
    
            int bin= Integer.parseInt(number,2);
            String hex = Integer.toHexString(bin);
    
            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.");
                }
        }
              int i= Integer.parseInt(str,2);
            System.out.println("Decimal:="+ i);
            System.out.println("Hexadecimal: "+ hex);
       }
     }
    Last edited by manibby93; 10-09-2013 at 06:48 PM.

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,534
    Rep Power
    5

    Default Re: I'm trying to convert binary to decimal & hexadecimal. Almost there??

    Quote Originally Posted by manibby93 View Post
    Lol. I could've sworn that I said that the binary to hex part doesn't work.
    Most folks come to this forum because their code doesn't work. That's a given. What was wanted was more specific information. Like when I enter this it prints this or behaves this way.

    Edit: You might also consider sprinkling some diagnostic print statements throughout your program.
    It may actually help you figure out what is going on.

    Regards,
    Jim
    Last edited by jim829; 10-09-2013 at 07:12 PM.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  9. #9
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,789
    Rep Power
    7

    Default Re: I'm trying to convert binary to decimal & hexadecimal. Almost there??

    Quote Originally Posted by manibby93 View Post
    I wrote it so that I could use it, lol. I don't know HOW to. Omg. Lol.
    Have mercy... I just mixed them together. I don't know. I just want to know how to make it WORK, not be questioned about why I copied & pasted something...
    My questions were aimed at getting you to think about your code, how it works and how it can be improved. I didn't mean for it to be harsh criticism.

  10. #10
    manibby93 is offline Member
    Join Date
    Sep 2013
    Posts
    77
    Rep Power
    0

    Default Re: I'm trying to convert binary to decimal & hexadecimal. Almost there??

    I'm mean, aren't I? lol. No, but seriously, how can I get both println statements to show on a separate line at the same time? I'll take trying to get me to think, lol.

  11. #11
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,789
    Rep Power
    7

    Default Re: I'm trying to convert binary to decimal & hexadecimal. Almost there??

    Post an example of the output you get and the output you expect instead.

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,950
    Rep Power
    19

    Default Re: I'm trying to convert binary to decimal & hexadecimal. Almost there??

    Quote Originally Posted by manibby93 View Post
    By poorly indented, you mean by me not using enough spaces b/w lines or comments? I added the spaces b/w lines...
    No, I mean the indentation.
    Look at this bit, the amount each line is indented is all over the place.
    Java Code:
            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.");
                }
        }
    Code should be indented such that you can spot where a while loop ends, and which closing bracket applies to which block of code.
    And the style of brackets should be consistent ie, opening brackets should either be on a new line or at the end of the previous line, not a mix.
    Java Code:
        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.");
            }
        }
    [/QUOTE]
    I have ONE more question. How can I get the two println statements to show at the same time? Right now, "Enter the Binary value to convert to Decimal:" shows first, then the next one. It may be an amateur question TO YOU, but I'm just trying to pass this class for credits... & I know little to nothing.
    [/QUOTE]

    I suspect (once you've clarified the problem) that you're going to be bumping up against the limitations of the command line.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 1
    Last Post: 09-17-2010, 12:40 PM
  2. Convert Decimal To Binary
    By aspire007 in forum New To Java
    Replies: 8
    Last Post: 08-06-2010, 07:32 AM
  3. Replies: 4
    Last Post: 10-26-2009, 10:17 PM
  4. Convert binary into decimal
    By WarmRegards in forum New To Java
    Replies: 8
    Last Post: 10-18-2009, 02:32 PM
  5. Replies: 7
    Last Post: 01-12-2008, 09:02 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
  •