i am trying to use recursion to find the occurence of a specific character in an array of characters

public class CharacterCounter
{
    public static void main(String [] args)
    {
        char test[] = {'a', 'b', 'a', 'c', 'd', 'a', 'e', 'a', 'f', 'g', 'h', 'a', 'i', 'a'};
        char searchChar = 'a';
        System.out.println(characterCounter(test, searchChar, test.length-1));

    }

    private static int characterCounter(char[] t, char sc, int index)
    {
        int count = 0;

        if(t.length < 0)
            return count;
        else
        {
            if(t[index] == sc)
                return count++;
            else
                return characterCounter(t, sc,index-1);
        }
    }
}

any help or suggestions would be appreciated.. thanks

What behavior are you expecting and what are you actually seeing? It's easier to help if you give specifics.

Meanwhile, here are some comments:

trying to use recursion

Is there a specific requirement that you use recursion? If not, there are other approaches I'd recommend exploring first.

to find the occurence of a specific character in an array of characters

Do you mean a) find the first occurrence, b) find the last occurrence, c) find any occurrence, or d) count the number of occurrences?

From the name characterCounter, I imagine it's d), but the code doesn't actually count occurrences of the given character.

characterCounter(test, searchChar, test.length-1)

I generally find starting at the beginning results in code that is easier to understand.

if(t.length < 0)

Arrays never have a length less than zero; not sure what you're trying to do with this.

yes, for this program i must use recursion.
d) count the number of occurences

yeah i have changed my code a little bit but still not getting correct answer here is my new code
public class CharacterCounter

public class CharacterCounter
{
    public static void main(String [] args)
    {
        char test[] = {'a', 'b', 'a', 'c', 'd', 'a', 'e', 'a', 'f', 'g', 'h', 'a', 'i', 'a'};
        char searchChar = 'a';

        System.out.println(characterCounter(test, searchChar, test.length));

    }

    private static int characterCounter(char[] t, char sc, int index)
    {
        int count=0;
        if(index == t.length)
            return count;
        else
        {
            if(t[index] == sc)
                return count++;
            else
                return characterCounter(t,sc,index+1);

        }
    }
}

i keep getting 0 as my answer

I think you can see why you get 0 as the answer if you trace your code by hand. It doesn't take many steps to find the problem. Your main calls characterCounter(test, searchChar, test.length). The first thing characterCounter does is check if index == t.length, but we know that index is test.length and t is test, so what we're really checking is test.length == test.length, which means characterCounter returns count which is 0.

Even if we assume you change something so that we can get past the first if of the first time characterCounter is called, there is a serious problem here: count++ is 0 since that is the value of count before it is incremented, and that means that characterCounter only ever returns 0. I guess you are misunderstanding something, because there is no point in incrementing a variable that will never be used again, and all local variables disappear when the method returns, so return count++ is exactly the same as return count.

Another issue is that if t[index] == sc you are returning without examining the rest of the array, which means there's no way you could be returning an accurate count in general, even if you weren't just returning 0 in all cases.

Edited 3 Years Ago by bguild

thank you!
here is my code that works.

public class CharacterCounter
{
    public static void main(String [] args)
    {
        char test[] = {'a', 'b', 'a', 'c', 'd', 'a', 'e', 'a', 'f', 'g', 'h', 'a', 'i'};
        char searchChar = 'a';

        System.out.println(characterCounter(test, searchChar, 0));

    }

    private static int characterCounter(char[] t, char sc, int index)
    {
        if(index == t.length)
            return 0;
        else
        {
            if(t[index] == sc)
                return 1+characterCounter(t,sc,index+1);
            else
                return characterCounter(t,sc,index+1);

        }
    }
}
This question has already been answered. Start a new discussion instead.