0

I have copied this code from a good answer on this website (counting characters in a String and returning the count) and have slightly amended it to suit my own needs. However, I seem to be getting an exception error in my method.

I would appreciate any help here.

Please forgive any errors in my code as I am still learning Java.

Here is my code:

public class CountTheChars {

public static void main(String[] args){

    String s = "Brother drinks brandy.";

    int countR = 0;

    System.out.println(count(s, countR));

}


public static int count(String s, int countR){

    char r = 0;

    for(int i = 0; i<s.length(); i++){

        if(s.charAt(i) == r){

            countR++;

        }

        return countR;

    }

}

}

Here is the exception:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
The method count(String) in the type CountTheChars is not applicable for the arguments (int)

at CountTheChars.main(CountTheChars.java:12)
3
  • 1
    What's the exception? Commented May 4, 2013 at 16:23
  • 1
    My guess is that you haven't recompiled your Eclipse project. Do so. Commented May 4, 2013 at 16:26
  • What does that mean exactly? I have never seen it before? Ahh, will do... Commented May 4, 2013 at 16:27

3 Answers 3

1

You are missing a return statement in your method public static int count(String s, int countR). Currently it does not return an int if s.length() == 0.

This should work as expected:

public class CountTheChars {

    public static void main(String[] args) {

        String s = "Brother drinks brandy.";

        int countR = 0;

        System.out.println(count(s, countR));

    }

    public static int count(String s, int countR) {

        for (int i = 0; i < s.length(); i++) {

            if (s.charAt(i) == 'r') {

                countR++;

            }

        }

        return countR;

    }

}
Sign up to request clarification or add additional context in comments.

3 Comments

Ahh, got it. My return was in the wrong place. Good spot and thank you =] Actually, I have amended where the return countR and it has removed the errors, however it is returning 0?
That's because your condition if(s.charAt(i) == r) is never true.
I see now. Thanks for helping me. It is now working fine.
1

2 issues:

  1. Your count method when doing the comparison of s.charAt(i) is comparing each letter in the word s to a variable called r that you made which you set as 0. Which means, technically your method is cOunting the number of times the number 0 occurs in your sentence. That is why you are getting 0. To fix that, remove your r variable and in your comparison, make s.charAt(i) == 'r' as the comparison. Take note of the apostrophe around the r Rio mean that you specifically refer to the character r.

  2. Your count method is not properly returning for cases where the string is nothing which means it will have a length of zero which means you're for loop is not run and your method will skip that as well as the return statement you have in there. To fix this issue move the return statement at the very bottom of the method so regardless of what string you get in, the return statement will always return (as it should because your method is expecting an int to be returned)

2 Comments

You have helped resolve my issue. It works and thanks.
No worries. If you could upvote this answer and select it as the answer, that'd be great:)
0

Why can not you use s.length() to count number of characters (i.e length of the string) in String s?

EDIT: changed to incorporate the "count number of occurances of char r". You call the function as count(s,countR, r) and declare char r = 'a' or whatever char you want in main

public static int count(String s, int countR, char r){

    countR= 0;
    for(int i = 0; i<s.length(); i++){

        if(s.charAt(i) == r){

            countR++;

        }

        return countR;

    }

}

3 Comments

The goal seems to be to count the occurrences of a particular character (looks like the number of 'r's) in the String, not the total length of the String.
That is correct. Count the amount of times the character 'r' is used in String s.
updated the code to do so.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.