reset password
Author Message
RandomAccess
Posts: 101
Posted 02:37 Nov 22, 2017 |

I keep getting an exception error when trying to test the following method.What's weird is that it works fine if the string has no characters in it like "", but put any characters in it like "a" and it returns the exception error. I firgured the if statement would catch the data before the recursive return, and I even tried switching them, but nothing works. It tells me that data is missing from the input and the recursive statement, which I don't get because I've set everything.  -

public int charaCount(String cc, int count){
        
        StringBuilder sb = new StringBuilder(cc);
        
        
        if(sb.toString() == "") return count;
        

        System.out.println(sb.toString() + " " + count);
        
        return charaCount(sb.deleteCharAt(0).toString(), count + 1);
        
    }

RandomAccess
Posts: 101
Posted 02:39 Nov 22, 2017 |

Also, can we use string.length() for the other recursive methods? I feel like that would help a lot.

jhurley
Posts: 207
Posted 07:02 Nov 22, 2017 |

1) don't take the count as a parameter, just have it return the count.  When the String is null, return 0

2) yes

RandomAccess
Posts: 101
Posted 21:39 Nov 22, 2017 |

doesn't count need to be initialized though? And when I tried it without it still gave an exception error and my test string read the counter as "0" when I had characters in the string.

RandomAccess
Posts: 101
Posted 00:02 Nov 23, 2017 |

I keep getting the error here. I don't know why. -

return charaCount(cc.substring(0, 0) + cc.substring(1));

jhurley
Posts: 207
Posted 08:29 Nov 23, 2017 |

just return 1 from the last recursive copy (the one in which the string has length 1) and add 1 as each of the other copies returns.  It is somewhat like the factorial algorithm, but a little simpler.

RandomAccess
Posts: 101
Posted 16:54 Nov 24, 2017 |

What would that do?

jhurley
Posts: 207
Posted 16:58 Nov 24, 2017 |

The length of a string is 1 plus the length of the string without the first character.

RandomAccess
Posts: 101
Posted 17:32 Nov 24, 2017 |

I thought we weren't supposed to use .length() for the first recursive method though?

RandomAccess
Posts: 101
Posted 23:44 Nov 24, 2017 |

Anyways, it keeps skipping my if statement that tells it when to go back, which would be if(cc == "") return count; and sometimes I even alter the method to add a " " at the beginning of the cc then change the if statement to if(cc == " ") return count; for some reason it always skips this statement. I even tried curly brackets to contain it. I don't know why it won't read and just continues into the loop until it gets to the unreadable "-1" length string.

jhurley
Posts: 207
Posted 09:11 Nov 25, 2017 |

Look at the return value of the factorial method again.

sdo8
Posts: 54
Posted 17:40 Nov 25, 2017 |

You initialize the count at 0.
Then you have if statements that return 0 if the string is null or has length less than 1
Once these statements are satisfied, you can add 1 to the count
When you have "return count + charaCount(newString);"
 where let's say, newString = "hello"

It should return (or look something like this): (count(1) + count(1)(from the charaCount(remove the first character from the string)), + count(1)(from the charaCount(remove the first character from the string again), then finally, when you call the final charaCount(where the new string is null or less than 1, it will return 0 so you're left with 1 + 1 + 1 + 1 + 1 + 0 = 5

hope this helps!
and I hope this isn't too much info!!

RandomAccess
Posts: 101
Posted 20:19 Nov 25, 2017 |

once again, aren't we not allowed to use .length() on the first recursive method?

RandomAccess
Posts: 101
Posted 22:13 Nov 25, 2017 |

Because that seems really weird to me. Why are we using the length of a string to find out the length of a string?

sdo8
Posts: 54
Posted 01:54 Nov 26, 2017 |

I think this assignment's main purpose is to practice recursion, and to learn how it works. Sure you can just return String.length() but that would ruin the whole purpose of this assignment. I believe you can use String.length() in your code, but make sure that you are returning a count and finding the length recursively. Think about it like this. If it is possible to remove the first character from your string, return 1 PLUS your method again BUT with the new string with the first character removed. Your method should keep being called until it's not possible to remove any more characters, then finally it will return a 0. When it returns 0, it'll be because you can't remove any more characters, and it won't call the method again because it's just "return 0", not return "0 + yourMethodHere("STRING")".

RandomAccess
Posts: 101
Posted 23:54 Nov 26, 2017 |

Works great now, thanks.

V_agu30
Posts: 8
Posted 14:21 Nov 28, 2017 |

Can you use return count + countCharacters(string.substring(1))?