Question... methods

Hi. I need to write a method called countA that accepts a String parameter & returns the # of times the character 'A' is found in the string.



  • The method must return a result, so you make a method similar like this:
    public static int countChars(){

    You have to get an argument with this method because you want specific String to be examined.
    You have to create a for loop that uses the "charAt" method that
    is part of the String API.
    charAt looks at the specific character at a certain point. The point is the "i" in a for loop, like this:
    for(int i = 0; i < string.length; i++){

    Make an "if" statement int the for loop that increases a integer variable every time the "charAt" finds a "A".

    Than the only thing you have to do is return the count variable.

    In another method create a integer variable that calls the just made method and gives a specific string with the method.

    I'm not gonna give you code, because I explained exactly what you have to, I hate if people ask for code because they are just too lazy to think of it themselves.

    Hope I helped you enouph.


Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


In this Discussion