Results 1 to 20 of 20
  1. #1
    herpeslurpy is offline Member
    Join Date
    Nov 2012
    Posts
    40
    Rep Power
    0

    Default Converting binary to decimal

    I wrote a program to turn convert binary input into a decimal number. I'm having trouble finding a problem in the code for it, as it only gives me a solution of 0.

    Java Code:
    package binaryConverter;
    import java.util.Scanner;
    
    public class binaryConverter {
    	public static void main(String[] args) {
    		String binary;
    		int i = 0;
    		Scanner input = new Scanner(System.in);
    		int array[] = {};
    		int secondArray[] = {128, 64, 32, 16, 8, 4, 2, 1};
    		int output = 0;
    		
    		binary = input.nextLine();
    		String binaryInput[] = binary.split("");
    		for(i = 0; i < binaryInput.length; i++){
    			if(binaryInput[i] == "0"){
    				array[i] = 0;
    			}else if(binaryInput[i] == "1"){
    				array[i] = secondArray[i];
    			}
    		}
    		for(int x = 0; x < array.length; x++){
    			output = output + array[x];
    		}
    		System.out.println(output);
    	}
    
    }
    The code works with multiple arrays. It takes binary input and splits it into an array, each value being 0 or 1. It then goes through the loop looking at those array places. secondArray holds the values for the specific bit of binary (128, 64, 32, so on). If the value of i the loop is on is 3, for example, and the input for that place is a 1, then the value of the third place in the array will be 32. Once all that is done, the second loop goes through and adds each point in the array together to find the final value, which is then displayed. I can't find any noticeable problem in the main code that converts, and I'm 95% sure the problem lies in the second for loop, and they're not adding properly. Any help would be appreciated
    I am not a clever man

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,598
    Rep Power
    5

    Default Re: Converting binary to decimal

    I haven't looked closely at your code to know if this is the problem. However, you are using == to compare strings ("0" and "1"). You should use equals. However, you might want to use the toCharArray method of String. Then you can compare characters using '0' and '1' with ==.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    herpeslurpy is offline Member
    Join Date
    Nov 2012
    Posts
    40
    Rep Power
    0

    Default Re: Converting binary to decimal

    Thanks for pointing out the need for .equals(), I managed to miss that somehow. I changed it, but it still doesn't work, it still returns 0. I figured out though, that the 0 being returned is the initial value of the output variable, so it tells me that the calculated values aren't being passed into output, or the calculation is always returning 0
    Last edited by herpeslurpy; 10-04-2013 at 04:28 PM.
    I am not a clever man

  4. #4
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,598
    Rep Power
    5

    Default Re: Converting binary to decimal

    You are indexing through an array (namely array) which has not been allocated any storage.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    herpeslurpy is offline Member
    Join Date
    Nov 2012
    Posts
    40
    Rep Power
    0

    Default Re: Converting binary to decimal

    Hmmm... I've tried giving the array a specific length, as well as initializing each value, but I still get 0
    I am not a clever man

  6. #6
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,598
    Rep Power
    5

    Default Re: Converting binary to decimal

    You should repost your code. Also, you should use Arrays.toString() to display your input string array. It may surprise you. And I also suggest you sprinkle println statements throughout your program to see if variables are being updated the way you think they are.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  7. #7
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,979
    Rep Power
    5

    Default Re: Converting binary to decimal

    String binaryInput[] = binary.split("");
    Huh? Split on no token at all (empty string) ?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

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

    Default Re: Converting binary to decimal

    It works, sorta. But not the best way. I suggested an alternative approach.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  9. #9
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,979
    Rep Power
    5

    Default Re: Converting binary to decimal

    Amazing Jim!

    Wait, I can target Star Trek jokes at you. A whole new world has just opened up for me.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  10. #10
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,598
    Rep Power
    5

    Default Re: Converting binary to decimal

    Actually, here is a better way. It employs a zero width positive look behind assertion. So it only splits on an empty string if the preceding character is a 1 or 0.
    Java Code:
     String binaryInput[] = binary.split("(?<=[01])");
    I still think the OP should have converted to a character array.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  11. #11
    herpeslurpy is offline Member
    Join Date
    Nov 2012
    Posts
    40
    Rep Power
    0

    Default Re: Converting binary to decimal

    So... Thanks guys, I actually managed to get it working, without changing it more or less, which is weird. Earlier, I start by removing the second for loop and make it calculate the decimal inside the second if statement. After that I added jim's suggestion of using (?<=[01]) to split it, which removed some out of bounds errors. I mean, that fixed it all.
    Java Code:
    import java.util.Scanner;
     
    public class binaryConverter {
        public static void main(String[] args) {
            String binary;
            int i = 0;
            Scanner input = new Scanner(System.in);
            int array[] = new int[8];
            int secondArray[] = {128, 64, 32, 16, 8, 4, 2, 1};
            int output = 0;
            
            binary = input.nextLine();
            String binaryInput[] = binary.split("(?<=[01])");
            for(i = 0; i < binaryInput.length; i++){
                if(binaryInput[i].equals("0")){
                    array[i] = 0;
                }else if(binaryInput[i].equals("1")){
                    array[i] = secondArray[i];
                    output = output + array[i];
                }
            }
            System.out.println(output);
        }
     
    }
    Thanks guys
    I am not a clever man

  12. #12
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,598
    Rep Power
    5

    Default Re: Converting binary to decimal

    Quote Originally Posted by herpeslurpy View Post
    I mean, that fixed it all.
    I don't think so (you did test this, right?). And if this is for a class, then I hope you can readily explain how you are splitting the string. Regular expressions are sort of in the advanced category and your teacher may wonder how you arrived at the solution.

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

  13. #13
    herpeslurpy is offline Member
    Join Date
    Nov 2012
    Posts
    40
    Rep Power
    0

    Default Re: Converting binary to decimal

    No, it's not an assignment. I got bored in class today and decided to do it, I'm amazed at how quickly I knocked out most of the code, though. And yes, I tested it thoroughly
    I am not a clever man

  14. #14
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,598
    Rep Power
    5

    Default Re: Converting binary to decimal

    So when you enter 110, what does it return (I am referencing your last posted code)?

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  15. #15
    herpeslurpy is offline Member
    Join Date
    Nov 2012
    Posts
    40
    Rep Power
    0

    Default Re: Converting binary to decimal

    Right now I have it set up so it only takes 8 bits of length, but as 00000110, it's 6. I know mah binary
    I am not a clever man

  16. #16
    herpeslurpy is offline Member
    Join Date
    Nov 2012
    Posts
    40
    Rep Power
    0

    Default Re: Converting binary to decimal

    Quote Originally Posted by herpeslurpy View Post
    Right now I have it set up so it only takes 8 bits of length, but as 00000110, it's 6. I know mah binary
    Well, I guess it can do less, but just 110 will return 192.
    I am not a clever man

  17. #17
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,598
    Rep Power
    5

    Default Re: Converting binary to decimal

    I thought it was free input. So I typed 110 and I got 192. I did not realize you were expecting leading zeroes. Perhaps a bad assumption
    on my part.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  18. #18
    herpeslurpy is offline Member
    Join Date
    Nov 2012
    Posts
    40
    Rep Power
    0

    Default Re: Converting binary to decimal

    Oh. I probably should've described it better, either way. But yeah, leading zeros, 8 bit max
    I am not a clever man

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

    Default Re: Converting binary to decimal

    Not a very flexible or user friendly program.

  20. #20
    herpeslurpy is offline Member
    Join Date
    Nov 2012
    Posts
    40
    Rep Power
    0

    Default Re: Converting binary to decimal

    Quote Originally Posted by Junky View Post
    Not a very flexible or user friendly program.
    Yeah, but I was just bored when I did it, it's not like converting binary is hard in the first place
    I am not a clever man

Similar Threads

  1. Converting binary to decimal using recursion
    By _inase in forum New To Java
    Replies: 4
    Last Post: 10-14-2011, 02:31 AM
  2. Replies: 5
    Last Post: 08-25-2011, 07:32 PM
  3. Converting characters to decimal to binary
    By Majeh in forum New To Java
    Replies: 4
    Last Post: 02-04-2011, 11:06 PM
  4. Algorithm for converting binary/hex to decimal
    By addictz04 in forum New To Java
    Replies: 2
    Last Post: 11-29-2010, 06:49 PM
  5. Replies: 3
    Last Post: 07-25-2009, 01:44 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
  •