Results 1 to 5 of 5
  1. #1
    _inase is offline Member
    Join Date
    Jan 2011
    Posts
    13
    Rep Power
    0

    Default Converting binary to decimal using recursion

    I made a method to convert from binary to decimal, but I also need to print an error message if the string I passed contains something other than 1's or 0's. I'm not sure where to implement that in my code...?

    Java Code:
    public int binToDec(String input){
    		
    	int size = input.length(); 
    	if(size == 1){			return Integer.parseInt(input); }
    	else{
    	return binToDec(input.substring(1,size)) + Integer.parseInt(input.substring(0,1))*(int)Math.pow(2, size-1); }
    
    	}
    Last edited by _inase; 10-13-2011 at 11:30 PM.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Converting binary to decimal using recursion

    What happens currently when you pass the method a "bad" string?

    -----

    A question to think about is where the message printing will take place: in the method, or by the caller of the method.

  3. #3
    _inase is offline Member
    Join Date
    Jan 2011
    Posts
    13
    Rep Power
    0

    Default Re: Converting binary to decimal using recursion

    Well currently it will just return a wrong value. Although now that you mention it, I don't know why I didn't think of just prompting the user for a string, checking it, and then calling the method if it's okay. Ha ha, cause I'm currently just hard coding the string.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Converting binary to decimal using recursion

    Have you actually tested and verified that it returns the wrong value?

    Testing the string and only calling the method for good strings is perfectly legitimate (how would you do the test?) But an alternative is to deal with what really happens when a bad string is passed.

  5. #5
    _inase is offline Member
    Join Date
    Jan 2011
    Posts
    13
    Rep Power
    0

    Default Re: Converting binary to decimal using recursion

    I think I got it. I wasn't taking into account that a bad string can include characters other than digits so I checked for that also and printed the message so it won't give me an error.

Similar Threads

  1. Replies: 5
    Last Post: 08-25-2011, 07:32 PM
  2. Converting characters to decimal to binary
    By Majeh in forum New To Java
    Replies: 4
    Last Post: 02-04-2011, 11:06 PM
  3. Decimal to binary, octal to decimal
    By matejm1994 in forum New To Java
    Replies: 3
    Last Post: 12-26-2010, 09:59 AM
  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
  •