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

• 10-09-2013, 08:28 AM
manibby93
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"
Code:

```int i= Integer.parseInt(str,2); System.out.println("Decimal:="+ i);```
Here is the full code:
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));   } }```
• 10-09-2013, 08:33 AM
Junky
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?
• 10-09-2013, 05:41 PM
manibby93
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...
• 10-09-2013, 05:56 PM
Tolls
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.
• 10-09-2013, 06:13 PM
manibby93
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.
• 10-09-2013, 06:22 PM
Tolls
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.
• 10-09-2013, 06:46 PM
manibby93
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.

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);   }  }```
• 10-09-2013, 07:08 PM
jim829
Re: I'm trying to convert binary to decimal & hexadecimal. Almost there??
Quote:

Originally Posted by manibby93
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.

Regards,
Jim
• 10-10-2013, 02:05 AM
Junky
Re: I'm trying to convert binary to decimal & hexadecimal. Almost there??
Quote:

Originally Posted by manibby93
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-2013, 02:29 AM
manibby93
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.
• 10-10-2013, 02:33 AM
Junky
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.
• 10-10-2013, 09:58 AM
Tolls
Re: I'm trying to convert binary to decimal & hexadecimal. Almost there??
Quote:

Originally Posted by manibby93
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.
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.
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.